16
Aug
07

Using HTTPService tag to send/receive variables to a server-side script

The following example demonstrates a very simple usage of sending parameters from Flex to a server-side script (written in ColdFusion) and then displaying the server response in our Flex application. The server-side script is a simple echo/”Hello world” type script, but it accepts URL or FORM variables, so you can send using the GET or POST HTTP-method.

It’s a pretty basic/crude example, but maybe it helps somebody out there.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/16/using-httpservice-tag-to-sendreceive-variables-to-a-server-side-script/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        creationComplete="httpService.send(myObj);">

    <!-- Parameters to send to remote script. -->
    <mx:Object id="myObj" name="peterd" />

    <mx:HTTPService id="httpService"
            url="http://www.flash-mx.com/mm/greeting.cfm"
            method="POST"
            resultFormat="flashvars" />

    <mx:Label text="{httpService.lastResult.welcomeMessage}" />

</mx:Application>

View source is enabled in the following example.

By popular request, here is the ColdFusion source code as well.

http://www.flash-mx.com/mm/greeting.cfm

<cfsilent>
<cfsetting enablecfoutputonly="Yes">
<cfif IsDefined("URL.name")><cfset Form.Name = URL.name /></cfif>
<cfif NOT IsDefined("Form.name") OR Len(Trim(Form.Name)) EQ 0>
    <cfset Form.Name = "[Mysterious Stranger]" />
</cfif>
</cfsilent><cfcontent reset="true" /><cfoutput>welcomeMessage=#UrlEncodedFormat("Welcome, "&Form.name)#</cfoutput>
<cfsetting enablecfoutputonly="No">

9 Responses to “Using HTTPService tag to send/receive variables to a server-side script”


  1. 1 ashok Oct 16th, 2007 at 2:50 am

    hi! nice to see this app

  2. 2 Patrick Whittingham Apr 9th, 2008 at 2:36 pm

    How do I have to different buttons, instead of putting this code (ie., click=”xmlRPC.send();” ). I want to call 2 different functions. One will pass hidden textinput field value as “1″ or “2″. This will tell CFMX to one of the 2 given CFC. httpservice calls a cfm page which cfinvokes one of the 2 cfc’s. httpservice use “POST” method.

  3. 3 Mas May 27th, 2008 at 11:24 pm

    Hi! Do you have the .cfm file? Is it possible if i take a look? Because i’m having problem how to write it.

  4. 4 peterd May 27th, 2008 at 11:53 pm

    Mas,

    I updated the entry above, hope that helps.

    Peter

  5. 5 Jake hawkes Nov 6th, 2008 at 8:50 am

    Hello Peter,

    Do you feel this would be a good way to send variables triggered by Cue Points in a flash video. I want to set a coupel cue points in a flash video and when they are triggered send a message that eventually chages out content in a HTML document.

    IE Content specific information…similiar to what you expect from CC but rich media.

    So a flash or Flex player is playing laong, hits a Cue Point which triggers my object and passes it a URL string. As above it sends it via MyObj to a MySQL database in my case. Then a JS object is listening for the object and populates a given content area with a SWF, JPG, Text or whatever that is in the data row…

    Am I on the right track? I have been trying to run this down for a week…spent all last night reading up on External Class…

  6. 6 Dylan Jan 11th, 2009 at 9:15 pm

    This has been a trying experience to fully understand how HTTPService and the supporting RPC (remote procedure call) classes operate. For those of you still having trouble, here is a solution I got working in Flex 3 returning simple XML after sending a login form using the POST method. It’s server side language independent.

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    
    	<mx:Script>
    		<![CDATA[
    
    	import mx.rpc.events.ResultEvent;
    	import mx.rpc.events.FaultEvent;
    
    	private function loginResult(event:ResultEvent):void{
    		// HTTP Service Succeeded
    		trace("Success " + event.type);
    
    	}
    
    	private function loginFault(event:FaultEvent):void{
    		// HTTP Service Failed
    		trace(event.type);
    		trace(event.fault);
    	}
    
    		]]>
    	</mx:Script>
    
    	<!--
    		resultFormat="e4x" was a huge step forward for me 
    
    		In this example my server receives the POST form variables email and password and validates the user to return the user_id and email_address .  The output from the server page that was POSTed to returns the following exactly as it is here:
    
    <results>
        <user_id><![CDATA[ 1 ]]></user_id>
        <email_address><![CDATA[ dylan.valade@pinelakedesign.com ]]></email_address> 
    
    </results> 
    
    	–>
    
    	<mx:HTTPService
    		resultFormat=”e4x”
    		method=”POST”
    		fault=”loginFault(event)”
    		result=”loginResult(event)”
    		url=”https://www.yourdomain.com/login/”
    		id=”loginService”
    		>
    
    	<mx:request>
             	<email>{email.text}</email>
    		<password>{password.text}</password>
          	</mx:request>
    
        </mx:HTTPService>
    
        <mx:VBox>
    
    	   <mx:TextInput id=”email” text=”dylan.valade@pinelakedesign.com” />
    
    	    <mx:TextInput id=”password” text=”secret” />
    
    		<mx:Button click=”loginService.send();” label=”Login” />
    
    		<mx:Text id=”resultText” text=”Result From Server: {loginService.lastResult.email_address}” >
    
    		</mx:Text>
    
    	</mx:VBox>
    
    </mx:Application>
    
  7. 7 Nash May 24th, 2009 at 3:46 pm

    Dylan-

    Thanks for example.

    Few ways in which variable can be passed to flex –mainly foccused for http request variables
    http://blog.flexexamples.com/2007/08/16/using-httpservice-tag-to-sendreceive-variables-to-a-server-side-script/

  8. 8 Nash May 24th, 2009 at 3:47 pm

    Peter/Dylan-

    Thanks for example.

    Few ways in which variable can be passed to flex –mainly foccused for http request variables
    http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html

  9. 9 Veronica May 25th, 2009 at 7:37 pm

    I need to say this: THANKS!!!… I’ve been trying so hard to find out how to get a single result from an xml text recieved by a servlet, on flex, and I finally found your example and IT WORKS!!! I’m so excited that I really want to thank you… BIG help!!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed