最近看一些文档,总结了一些<mx:HTTPService>给后台传递参数的方法,列举如下:
方法1:采用URLVariables对象
<?
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 httpEncoding0(param:String):String{
return param;//encodeURI(param);
}
private function doRequest():void{
btn_do.enabled=false;
var url:String = "http://localhost:8600/grid.jsp";
//以下那样写后台会乱码,不管是否做URI编码转换
//url += "?user="+httpEncoding0("用户名");
//url += "&psw="+httpEncoding0("密码");
//trace(url);
srv.url = url;
//srv.send();
//以下这样写正常
var params:URLVariables = new URLVariables();
//这个user,psw就是传入后台的参数user,jsp就用 request.getParameter("user")来取
params.user = httpEncoding("用户名");
params.psw = httpEncoding("密码");
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 ="测试与jsp后台交互" 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 >
< 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 httpEncoding0(param:String):String{
return param;//encodeURI(param);
}
private function doRequest():void{
btn_do.enabled=false;
var url:String = "http://localhost:8600/grid.jsp";
//以下那样写后台会乱码,不管是否做URI编码转换
//url += "?user="+httpEncoding0("用户名");
//url += "&psw="+httpEncoding0("密码");
//trace(url);
srv.url = url;
//srv.send();
//以下这样写正常
var params:URLVariables = new URLVariables();
//这个user,psw就是传入后台的参数user,jsp就用 request.getParameter("user")来取
params.user = httpEncoding("用户名");
params.psw = httpEncoding("密码");
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 ="测试与jsp后台交互" 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 >
方法2:采用<mx:request/>,同时也演示了mx:State的用法,[来自网上]
<?
xml version="1.0" encoding="utf-8"
?>
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="absolute" >
< mx:states >
< mx:State name ="Logged In" >
< mx:SetProperty target ="{panel1}" name ="width" value ="95%" />
< mx:SetProperty target ="{panel1}" name ="height" value ="95%" />
< mx:RemoveChild target ="{password}" />
< mx:RemoveChild target ="{username}" />
< mx:RemoveChild target ="{label1}" />
< mx:RemoveChild target ="{Submit}" />
< mx:RemoveChild target ="{label2}" />
< mx:SetProperty target ="{panel1}" name ="title" value ="Members Section" />
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="10" text ="Welcome to the Members Section!" />
</ mx:AddChild >
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="36" text ="Here you can do great things, like join the forums @ Viper Creations!" />
</ mx:AddChild >
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="62" text ="Label" />
</ mx:AddChild >
</ mx:State >
</ mx:states >
< mx:Script >
<![CDATA[
import mx.rpc.events.ResultEvent;
]]>
</ mx:Script >
< mx:Script >
<![CDATA[
private function checkLogin(evt:ResultEvent):void
{
if(evt.result.loginsuccess == "yes")
{
currentState = "Logged In";
}
if(evt.result.loginsuccess == "no")
{
mx.controls.Alert.show('Invalid username/password');
}
}
]]>
</ mx:Script >
< mx:HTTPService id ="login_user" result ="checkLogin(event)" showBusyCursor ="true" method ="POST" url ="http://www.vipercreations.com/site_admin/login.php" useProxy ="false" >
< mx:request xmlns ="" >
< username >
{username.text}
</ username >
< password >
{password.text}
</ password >
</ mx:request >
</ mx:HTTPService >
< 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 ="login_user.send();" />
</ mx:Panel >
</ mx:Application >
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="absolute" >
< mx:states >
< mx:State name ="Logged In" >
< mx:SetProperty target ="{panel1}" name ="width" value ="95%" />
< mx:SetProperty target ="{panel1}" name ="height" value ="95%" />
< mx:RemoveChild target ="{password}" />
< mx:RemoveChild target ="{username}" />
< mx:RemoveChild target ="{label1}" />
< mx:RemoveChild target ="{Submit}" />
< mx:RemoveChild target ="{label2}" />
< mx:SetProperty target ="{panel1}" name ="title" value ="Members Section" />
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="10" text ="Welcome to the Members Section!" />
</ mx:AddChild >
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="36" text ="Here you can do great things, like join the forums @ Viper Creations!" />
</ mx:AddChild >
< mx:AddChild relativeTo ="{panel1}" position ="lastChild" >
< mx:Label x ="10" y ="62" text ="Label" />
</ mx:AddChild >
</ mx:State >
</ mx:states >
< mx:Script >
<![CDATA[
import mx.rpc.events.ResultEvent;
]]>
</ mx:Script >
< mx:Script >
<![CDATA[
private function checkLogin(evt:ResultEvent):void
{
if(evt.result.loginsuccess == "yes")
{
currentState = "Logged In";
}
if(evt.result.loginsuccess == "no")
{
mx.controls.Alert.show('Invalid username/password');
}
}
]]>
</ mx:Script >
< mx:HTTPService id ="login_user" result ="checkLogin(event)" showBusyCursor ="true" method ="POST" url ="http://www.vipercreations.com/site_admin/login.php" useProxy ="false" >
< mx:request xmlns ="" >
< username >
{username.text}
</ username >
< password >
{password.text}
</ password >
</ mx:request >
</ mx:HTTPService >
< 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 ="login_user.send();" />
</ mx:Panel >
</ mx:Application >