博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flex HTTPService如何给后台传递参数
阅读量:2426 次
发布时间:2019-05-10

本文共 3757 字,大约阅读时间需要 12 分钟。

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

转载地址:http://jbfmb.baihongyu.com/

你可能感兴趣的文章
“编程能力差的程序员,90%输在这事上!”谷歌AI专家:都是瞎努力!
查看>>
张一鸣做电商:再造一个“抖音”
查看>>
“你写的 Bug 让我来改好吗” | 每日趣闻
查看>>
大厂技术文档:Redis+Nginx+Spring全家桶+Dubbo精选
查看>>
笑死,别再黑程序员了好吗? | 每日趣闻
查看>>
Python 爬取 13966 条运维招聘信息,这些岗位最吃香
查看>>
28 岁退休程序员自述:不是富二代,行政专业出身,非典型程序员
查看>>
那时刚毕业的我,曾参与惊心动魄 3Q 大战
查看>>
程序员爬取 5000+ 口红商品数据,差点比女朋友更懂口红?
查看>>
30 张图解 | 高频面试知识点总结:面试官问我高并发服务模型哪家强?
查看>>
以太坊创始人V 神:普通人看见现在,天才看见未来
查看>>
厉害!从电影花瓶到 Wi-Fi 之母,这才是乘风破浪的姐姐!
查看>>
中国开源大爆发进行时,你没掉队吧?
查看>>
用 Python 实现抖音上的“人像动漫化”特效,原来这么简单!
查看>>
一周内咸鱼疯转 2.4W 次,最终被所有大厂封杀!
查看>>
关于鸿蒙 2.0,那些开发者不知道的一切
查看>>
Google 排名第一的语言,引数十万人关注:搞定它,技术大牛都甘拜下风
查看>>
JavaScript 爆红后,微软为何还要开发 TypeScript?
查看>>
软件开发行业,年轻与大龄程序员的生存现状
查看>>
王者荣耀活动精选 Blink 第二弹来袭!
查看>>