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