September 24th, 2007 by Kyle
Tags: cfc, cfcs, cfmx7.0.2, ColdFusion, fds, Flex, Flex Builder, flex-2.0, flex-data-services, flex-sdk, flex2.0.1, host-my-site, hostmysite, hostmysite.com, mxml, remote-object, remoteobject, rpc, web-services, webservices
Posted in: ColdFusion, Flex
One of my first blog posts pointed to a dev center article that I wrote on this topic for the Flex 2 release. I often send customers to look at this article when appropriate as it is a good starting point for webservice and remoteobject communication between Flex and Coldfusion. I also find myself referring to this sample once in a while and so I have finally decided to host the sample and cfc from my website/blog and potentially enhance the cfc to support more use cases for subsequent blog posts.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" >
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
[Bindable]
public var sResult:String;
public function handleStringResult(event:ResultEvent):void{
sResult=event.result as String;
}
]]>
</mx:Script>
<mx:WebService id="myService"
useProxy="false"
wsdl="http://739saintlouis.com/services/HelloWorld.cfc?wsdl"
showBusyCursor="true">
<mx:operation name="sayHelloString" result="handleStringResult(event)" fault="Alert.show(event.fault.message)"/>
</mx:WebService>
<mx:Label id="lblStringResult" text="{sResult}"/>
<mx:Button label="get String via Webservice" click="myService.sayHelloString.send()"/>
</mx:Application>
Browse the source of this example.
Download a zipfile containing the source to this sample.
Here is the code and sample that demonstrates retrieving more datatypes from coldfusion:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" >
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
[Bindable]
public var sResult:String;
[Bindable]
public var aResult:ArrayCollection;
[Bindable]
public var oResult:Object;
[Bindable]
public var qResult:ArrayCollection;
[Bindable]
public var aReturnTypes: Array = [ {label:"string", data:"string"},
{label:"array", data:"array"}, {label:"struct", data:"struct"}, {label:"query", data:"query"} ];
[Bindable]
public var returnType:String="string";
public function clearAll():void{
sResult=new String();
aResult=new ArrayCollection();
oResult=new Object();
qResult=new ArrayCollection();
}
public function handleStringResult(event:ResultEvent):void{
sResult=event.result as String;
}
public function handleArrayResult(event:ResultEvent):void{
aResult=event.result as ArrayCollection;
}
public function handleStructResult(event:ResultEvent):void{
oResult=event.result as Object;
}
public function handleQueryResult(event:ResultEvent):void{
qResult=event.result as ArrayCollection;
}
public function handleAnyTypeResult(event:ResultEvent):void{
switch (returnType) {
case ’string’:
handleStringResult(event);
break ;
case ‘array’:
handleArrayResult(event);
break ;
case ’struct’:
handleStructResult(event);
break ;
case ‘query’:
handleQueryResult(event);
break ;
default:
handleStringResult(event);
break ;
}
}
]]>
</mx:Script>
<mx:WebService id="myService"
useProxy="false"
wsdl="http://739saintlouis.com/services/HelloWorld.cfc?wsdl"
showBusyCursor="true">
<mx:operation name="sayHelloString" result="handleStringResult(event)" fault="Alert.show(event.fault.message)"/>
<mx:operation name="sayHelloArray" result="handleArrayResult(event)" fault="Alert.show(event.fault.message)"/>
<mx:operation name="sayHelloStruct" result="handleStructResult(event)" fault="Alert.show(event.fault.message)"/>
<mx:operation name="sayHelloQuery" result="handleQueryResult(event)" fault="Alert.show(event.fault.message)"/>
<mx:operation name="sayHelloAnyType" result="handleAnyTypeResult(event)" fault="Alert.show(event.fault.message)"/>
</mx:WebService>
<mx:Button label="get String Webservice" click="myService.sayHelloString.send()"/>
<mx:Label id="lblStringResult" text="{sResult}"/>
<mx:Button label="get Array Webservice" click="myService.sayHelloArray.send()"/>
<mx:List dataProvider="{aResult}" width="150"/>
<mx:Button label="get Struct Webservice" click="myService.sayHelloStruct.send()"/>
<mx:Form >
<mx:FormHeading label="How do you say {oResult.ENGLISH} in Latin?"/>
<mx:FormItem>
<mx:RadioButtonGroup id="language" />
<mx:RadioButton groupName="language" id="lFrench" value="french"
label="{oResult.FRENCH}" width="150" />
<mx:RadioButton groupName="language" id="lSpanish" value="spanish"
label="{oResult.SPANISH}" width="150" />
<mx:RadioButton groupName="language" id="lLatin" value="latin"
label="{oResult.LATIN}" width="150" />
</mx:FormItem>
</mx:Form>
<mx:Button label="get Query Webservice" click="myService.sayHelloQuery.send()"/>
<mx:DataGrid dataProvider="{qResult}">
<mx:columns>
<mx:Array>
<mx:DataGridColumn dataField="hello"/>
<mx:DataGridColumn dataField="world"/>
<mx:DataGridColumn dataField="exclamation"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<mx:Button label="get >" click="returnType=cbo.selectedItem.data;myService.sayHelloAnyType.send(returnType);"/>
<mx:ComboBox id="cbo" dataProvider="{aReturnTypes}"/>
</mx:HBox>
<mx:Button label="clear all" click="clearAll()"/>
</mx:Application>
2 Comments »











February 28th, 2008 at 7:35 pm
Hi there
Nice tutorial. I am looking for information on passing information from Flex 2 to ColdFusion – have tried googling for information on this but keep coming up with nothing very substantial.
Wondering if you might be able to point me to information on how to do this.
Regards & thanks in advance.
Peter
March 10th, 2008 at 2:34 pm
You should try using the Coldfusion wizards in Flex Builder. They build a simple crud application with a CF backend that should help demonstrate how to communicate with CF.