Flex中利用HTTPService和服务器端脚本进行数据通信的例子

By Minidxer | July 23, 2008

接下来的例子演示了从Flex中发送一个参数给服务器端的ColdFusion脚本,然后将接收到的返回值显示在Flex应用中。服务器端的脚本是一个简单的”Hello world” 类型的脚本,但是可以接受URL或者FORM内的值,所以可以通过发送GET或者POST的HTTP事件。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         creationComplete="httpService.send(myObj);">
  5.  
  6.     <!-- Parameters to send to remote script. -->
  7.     <mx:Object id="myObj" name="peterd" />
  8.  
  9.     <mx:HTTPService id="httpService"
  10.             url="http://www.flash-mx.com/mm/greeting.cfm"
  11.             method="POST"
  12.             resultFormat="flashvars" />
  13.  
  14.     <mx:Label text="{httpService.lastResult.welcomeMessage}" />
  15.  
  16. </mx:Application>
下面是服务器端ColdFusion部分的代码:
  1. <cfsilent>
  2. <cfsetting enablecfoutputonly="Yes">
  3. <cfif IsDefined("URL.name")><cfset Form.Name = URL.name /></cfif>
  4. <cfif NOT IsDefined("Form.name") OR Len(Trim(Form.Name)) EQ 0>
  5.     <cfset Form.Name = "[Mysterious Stranger]" />
  6. </cfif>
  7. </cfsilent><cfcontent reset="true" /><cfoutput>welcomeMessage=#UrlEncodedFormat("Welcome, "&Form.name)#</cfoutput>
  8. <cfsetting enablecfoutputonly="No">
代码:Peter deHaan 翻译/整理/编译:minidxer

Topics: Flex | Tags: , , , , ,

Search Posts

Archives

Sponsored Ads