Flex HTTPService如何给后台传递参数

最近看一些文档,总结了一些<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 >

 方法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 >
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值