Flex HTTPService 提交表单

关键字: flex数据交换

<mx:HTTPService>给后台传递参数的方法,列举如下:

方法1:采用URLVariables对象

Mxml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.   
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    
  4.   
  5.      layout="absolute" fontSize="12">    
  6.   
  7.     <mx:Script>    
  8.   
  9.         <![CDATA[    
  10.   
  11.             import mx.controls.Alert;    
  12.   
  13.             import mx.rpc.events.ResultEvent;    
  14.   
  15.             //对提交给后台的参数进行UTF-8的编码处理    
  16.   
  17.             private function httpEncoding(param:String):String{    
  18.   
  19.                 return encodeURIComponent(param);    
  20.   
  21.             }    
  22.   
  23.             private function httpEncoding0(param:String):String{    
  24.   
  25.                 return param;//encodeURI(param);    
  26.   
  27.             }    
  28.   
  29.             private function doRequest():void{    
  30.   
  31.                 btn_do.enabled=false;    
  32.   
  33.                 var url:String = "http://localhost:8600/grid.jsp";    
  34.   
  35.                 //以下那样写后台会乱码,不管是否做URI编码转换    
  36.   
  37.                 //url += "?user="+httpEncoding0("用户名");    
  38.   
  39.                 //url += "&psw="+httpEncoding0("密码");    
  40.   
  41.                 //trace(url);    
  42.   
  43.                 srv.url = url;    
  44.   
  45.                 //srv.send();    
  46.   
  47.                 //以下这样写正常    
  48.   
  49.                 var params:URLVariables = new URLVariables();    
  50.   
  51.                 //这个user,psw就是传入后台的参数user,jsp就用 request.getParameter("user")来取    
  52.   
  53.                 params.user = httpEncoding("用户名");    
  54.   
  55.                 params.psw = httpEncoding("密码");    
  56.   
  57.                 srv.send(params);               
  58.   
  59.             }    
  60.   
  61.             private function resultHandler(event:ResultEvent):void{    
  62.   
  63.                 Alert.show("与后台交互结束,前台开始取得的数据...","提示信息");    
  64.   
  65.                 btn_do.enabled=true;    
  66.   
  67.             }    
  68.   
  69.         ]]>    
  70.   
  71.     </mx:Script>    
  72.   
  73.     <mx:HTTPService id="srv" result="resultHandler(event);"/>    
  74.   
  75.     <mx:Panel title="测试与jsp后台交互" layout="absolute" width="100%" height="90%">    
  76.   
  77.         <mx:Button id="btn_do" label="取得数据" click="doRequest();"/>    
  78.   
  79.         <mx:Spacer height="1"/>    
  80.   
  81.         <mx:DataGrid dataProvider="{srv.lastResult.catalog.product}" width="100%" height="100%" y="28"/>        
  82.   
  83.     </mx:Panel>    
  84.   
  85. </mx:Application>   

 
方法2:采用<mx:request/>,同时也演示了mx:State的用法,[来自网上]

Mxml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.   
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">    
  4.   
  5.     <mx:states>    
  6.   
  7.         <mx:State name="Logged In">    
  8.   
  9.             <mx:SetProperty target="{panel1}" name="width" value="95%"/>    
  10.   
  11.             <mx:SetProperty target="{panel1}" name="height" value="95%"/>    
  12.   
  13.             <mx:RemoveChild target="{password}"/>    
  14.   
  15.             <mx:RemoveChild target="{username}"/>    
  16.   
  17.             <mx:RemoveChild target="{label1}"/>    
  18.   
  19.             <mx:RemoveChild target="{Submit}"/>    
  20.   
  21.             <mx:RemoveChild target="{label2}"/>    
  22.   
  23.             <mx:SetProperty target="{panel1}" name="title" value="Members Section"/>    
  24.   
  25.             <mx:AddChild relativeTo="{panel1}" position="lastChild">    
  26.   
  27.                 <mx:Label x="10" y="10" text="Welcome to the Members Section!"/>    
  28.   
  29.             </mx:AddChild>    
  30.   
  31.             <mx:AddChild relativeTo="{panel1}" position="lastChild">    
  32.   
  33.                 <mx:Label x="10" y="36" text="Here you can do great things, like join the forums @ Viper Creations!"/>    
  34.   
  35.             </mx:AddChild>    
  36.   
  37.             <mx:AddChild relativeTo="{panel1}" position="lastChild">    
  38.   
  39.                 <mx:Label x="10" y="62" text="Label"/>    
  40.   
  41.             </mx:AddChild>    
  42.   
  43.         </mx:State>    
  44.   
  45.     </mx:states>    
  46.   
  47.     <mx:Script>    
  48.   
  49.         <![CDATA[    
  50.   
  51.             import mx.rpc.events.ResultEvent;               
  52.   
  53.         ]]>    
  54.   
  55.     </mx:Script>    
  56.   
  57.     <mx:Script>    
  58.   
  59. <![CDATA[    
  60.   
  61.   
  62. private function checkLogin(evt:ResultEvent):void    
  63. {    
  64.   
  65.     if(evt.result.loginsuccess == "yes")    
  66.     {    
  67.         currentState = "Logged In";    
  68.   
  69.     }    
  70.   
  71.   
  72.   
  73.     if(evt.result.loginsuccess == "no")    
  74.     {    
  75.         mx.controls.Alert.show('Invalid username/password');    
  76.     }           
  77.   
  78. }    
  79.   
  80. ]]>    
  81.   
  82. </mx:Script>    
  83.   
  84.     <mx:HTTPService id="login_user" result="checkLogin(event)" showBusyCursor="true" method="POST" url="http://www.vipercreations.com/site_admin/login.php" useProxy="false">    
  85.   
  86.         <mx:request xmlns="">    
  87.             <username>    
  88.                 {username.text}    
  89.             </username>    
  90.   
  91.             <password>    
  92.                 {password.text}    
  93.             </password>    
  94.   
  95.         </mx:request>    
  96.   
  97.     </mx:HTTPService>    
  98.       
  99.   
  100.     <mx:Panel resizeEffect="Resize" width="250" height="200" layout="absolute" title="Login System" horizontalCenter="0" verticalCenter="-2" id="panel1">    
  101.   
  102.         <mx:Label x="10" y="10" text="Username:" id="label1"/>    
  103.   
  104.         <mx:TextInput x="10" y="36" id="username"/>    
  105.   
  106.         <mx:Label x="10" y="66" text="Password:" id="label2"/>    
  107.   
  108.         <mx:TextInput x="10" y="92" id="password" displayAsPassword="true"/>    
  109.   
  110.         <mx:Button x="10" y="122" label="Submit" id="Submit" click="login_user.send();"/>    
  111.   
  112.     </mx:Panel>    
  113.   
  114. </mx:Application>   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值