Flex与服务器端交换数据最常见的方法就是使用HTTPService类。
【ActionScript 3.0 Language and Components Reference】是这样定义HTTPService类的:
在 MXML 文件中使用
<mx:HTTPService>
标签代表 HTTPService 对象。当调用 HTTPService 对象的 send()
方法时,将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选择向指定 URL 传递参数。如果没有使用基于服务器的代理服务,则只能使用 HTTP GET 或 POST 方法。如果将 useProxy 属性设置为 true 并使用基于服务器的代理服务,则还可以使用 HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。 注意:由于软件限制,当使用 GET 时 HTTPService 不生成用户界面友好的错误消息。
MXML 语法如下:
1.使用HTTPService请求数据
<mx:HTTPService id= "feedRequest" url= "http://weblogs.macromedia.com/mchotin/index.xml" useProxy="false" />
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx=" " layout="absolute" creationComplete="feedRequest.send()"> <mx:HTTPService id="feedRequest" url=" " useProxy="false" /> <mx:Panel title="{feedRequest.lastResult.rss.channel.title}" width="431" height="374" y="69" layout="absolute" horizontalCenter="0"> <mx:DataGrid id="datePanel" x="10" y="10" dataProvider="{feedRequest.lastResult.rss.channel.item}" width="391"> <mx:columns> <mx:DataGridColumn headerText="Column 1" dataField="title"/> <mx:DataGridColumn headerText="Column 2" dataField="description"/> <mx:DataGridColumn headerText="Column 3" dataField="link"/> </mx:columns> </mx:DataGrid> <mx:TextArea htmlText="{datePanel.selectedItem.pubDate}" y="174" left="10" right="10"> </mx:TextArea> <mx:LinkButton label="Go" x="139" y="243" click="navigateToURL(new URLRequest(datePanel.selectedItem.link))"/> </mx:Panel> </mx:Application>
<mx:HTTPService id="userRequest" url=" " useProxy="false" method="POST"> <mx:request xmlns=""> <username>{username.text}</username><password>{pass.text}</password> </mx:request> </mx:HTTPService> <mx:Form x="22" y="10" width="356"> <mx:HBox> <mx:Label text="Username"/> <mx:TextInput id="username"/> </mx:HBox> <mx:HBox> <mx:Label text="PassWord"/> <mx:TextInput id="pass"/> </mx:HBox> <mx:Button label="Submit" click="userRequest.send()"/> </mx:Form>