一.基于GET的HTTPService:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
fontSize="12">
<mx:Script>
<![CDATA[
import
mx.controls.Alert;
import mx.rpc.events.ResultEvent;
//对提交给后台的参数进行UTF-8的编码处理
private function
httpEncoding(param:String):String{
return
encodeURIComponent(param);
}
private function
doRequest():void{
btn_do.enabled=false;
var url:String =
"http://localhost:1337";
srv.url = url;
var params:URLVariables = new URLVariables();
params.user
= "fuland";
params.psw = "123456";
srv.send(params);
}
private function
resultHandler(event:ResultEvent):void{
Alert.show("与后台交互结束,前台开始取得的数据...","提示信息");
btn_do.enabled=true;
}
]]>
</mx:Script>
<mx:HTTPService id="srv" result="resultHandler(event);"/>
<mx:Panel title="测试与后台交互" layout="absolute" width="100%"
height="90%">
<mx:Button id="btn_do" label="取得数据"
click="doRequest();"/>
<mx:Spacer
height="1"/>
<mx:DataGrid
dataProvider="{srv.lastResult.catalog.product}" width="100%" height="100%"
y="28"/>
</mx:Panel>
</mx:Application>
二.基于POST的HTTPService:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
fontSize="12">
<mx:Script>
<![CDATA[
import
mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import
flash.display.MovieClip;
import
flash.events.MouseEvent;
import flash.text.TextField;
import
flash.text.TextFormat;
private function
checkLogin(evt:ResultEvent):void
{
var mesh:XML = evt.result
as XML;
var str:String =
evt.result.toString();
Alert.show(str);
}
private
function sendLogin(e:MouseEvent):void
{
var
employees:XML =
<employees>
<employee
ssn="123-123-1234">
<name first="John"
last="Doe"/>
<address>
<street>11
Main St.</street>
<city>San
Francisco</city>
<state>CA</state>
<zip>98765</zip>
</address>
</employee>
<employee
ssn="789-789-7890">
<name first="Mary"
last="Roe"/>
<address>
<street>99
Broad
St.</street>
<city>Newton</city>
<state>MA</state>
<zip>01234</zip>
</address>
</employee>
</employees>;
login_user.contentType=‘application/xml‘;
//告诉服务器发送的格式
login_user.send(employees);
}
]]>
</mx:Script>
<mx:HTTPService id="login_user" result="checkLogin(event)"
showBusyCursor="true" method="POST" resultFormat="e4x"
url="http://127.0.0.1:1337" useProxy="false"/>
//<mx:HTTPService id="login_user" result="checkLogin(event)"
showBusyCursor="true" method="POST" resultFormat="xml"
url="http://127.0.0.1:1337" useProxy="false"/>
如果有要在相应中得到XML对象则应该选择e4x,result就是Object类型 传“xml”的时候,用as转换无法成功转成xml,则返回null,但如果用强制转换XML(event.result).就可以成功。
<mx:Panel resizeEffect="Resize" width="250" height="200"
layout="absolute" title="Login System" horizontalCenter="0" verticalCenter="-2"
id="panel1">
<mx:Label x="10" y="10" text="Username:"
id="label1"/>
<mx:TextInput x="10" y="36"
id="username"/>
<mx:Label x="10" y="66" text="Password:"
id="label2"/>
<mx:TextInput x="10" y="92" id="password"
displayAsPassword="true"/>
<mx:Button x="10" y="122" label="Submit"
id="Submit"
click="sendLogin(event)"/>
</mx:Panel>
</mx:Application>
基于Flex的HTTPService(GET和POST),布布扣,bubuko.com