Article
Supercharge Your Flex App with ColdFusion Power
Sending Request Parameters
Perhaps you need to send parameters to your service. The easiest way to pass in parameters is by adding an mx:request tag as a nested tag of the HTTP service. Let’s imagine we need to send two parameters, dataMethod and userType, to a ColdFusion script. We’d use the mx:request element like so:
<mx:HTTPService id="dataService" url="http://example.com/sendDataRequest.cfm" method="POST">
<mx:request>
<dataMethod>getAll</dataMethod>
<userType>administrator</userType>
</mx:request>
</mx:HTTPService>
Since we’re using the HTTP POST method, all the request variables we’re creating here will become ColdFusion variables in ColdFusion’s FORM scope. On the ColdFusion side, we’re therefore going to end up with FORM.dataMethod as well as FORM.userType on the ColdFusion template. If you’d chosen HTTP GET (by specifying method="GET"), your request data would become URL variables: URL.userType and URL.dataMethod in this case.
So far we’ve just looked into returning XML data via HTTP services from ColdFusion templates. Although that’s a very common way to interact with HTTP services and ColdFusion, there are some other, alternative return formats for HTTP services that in some occasions might be more appropriate to use:
object: response data is XML and will be converted into a tree of ActionScript objects (ArrayCollection,ObjectProxy)xml: response data is XML and will be converted into an ActionScript object of typeXMLnode—this is a legacy format which is only here for compatibility; it’s best to avoid using it.e4x: response data is XML and will be converted into an ActionScript XML objectflashvars: response data is a chain of key/value-pairs:name1=value1&'name2=value2, and so ontext: response data is text, no conversion happens.
resultFormat="object" is the default, and is quite often the right way to go. If you want to work with XML instead of ArrayCollections and ObjectProxy, e4x (ECMAScript for XML) is the preferred result format—xml makes use of a deprecated set of API classes that’s part of Flex for compatibility reasons only.
Providing a Service with ColdFusion
So: we've spent a good amount of this article talking about HTTP services and the general structure of a service. Now that we understand how these work, discussing web services and remote objects becomes much easier.
Let’s take the following ColdFusion component that offers one simple echo method to the outside world, and try to build a Flex client that hooks into that CFC:
<cfcomponent>x
<cffunction
name="echo"
returntype="string"
access="remote"
output="false"
hint="I echo whatever I'm being passed">
<cfargument name="input" type="string" required="true">
<cfreturn "Echoing..." & arguments.input>
</cffunction>
</cfcomponent>
ColdFusion offers two ways of exposing the functionality of that component’s method to a caller: as a SOAP-based web service or as a remote object. Let’s try each.