Flex HTTPService如何给后台传递参数

阅读更多
最近看一些文档,总结了一些给后台传递参数的方法,列举如下:

方法1:采用URLVariables对象




     layout="absolute" fontSize="12"
    >
   
                    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;
            }
        ]]>
   

   
   
       
       
            
   


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





   
       
           
           
           
           
           
           
           
           
           
               
           

           
               
           

           
               
           

       

   

   
                    import mx.rpc.events.ResultEvent;
           
        ]]>
   

   
   


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'');

    }       
}

]]>


   
       
           
                {username.text}
           

           
                {password.text}
           

       

   

   
   
       
       
       
       
       
   

   

你可能感兴趣的:(Flex,JSP,XML)