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">

4 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

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;".