flex通过非AmfPHP途径与后台交互

有时候只需要一点通信,感觉用AmfPHP是大材小用了,或者你急用,来不及配置AmfPHP了,可以使用以下这些轻量级的通信方式。
由于WebService我们暂时用不到,在这里不举例了。

1.httpService
不多说了,直接上代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
 3    <mx:Script>
 4        <![CDATA[
 5            internal function doResult():void{
 6                //返回结果存储在HttpService的lastResult中。需要通过id调用
 7                var returnValue:String=callphp.lastResult.toString();
 8                txt.text = returnValue;
 9            }
10        ]]>
11    </mx:Script>
12      <mx:HTTPService showBusyCursor="true" id="callphp" result="doResult()" method="POST" url="http://192.168.100.100/flex/testphp/getIp.php">
13         <mx:request> 
14             <message> <!--这里是参数的名称-->
15                {txt.text}<!--这里是参数的内容-->
16             </message> 
17         </mx:request> 
18    </mx:HTTPService>
19      <mx:TextArea id="txt" x="272" y="48" height="78" width="192"/>
20      <mx:Button x="335.5" y="134" label="callphp" click="callphp.send()"/><!--直接调用HttpService的id的send方法-->
21</mx:Application>
如果不传递参数,HttpService的标签中间不包含任何东西即可。

2.URLLoader+URLRequest+URLVariables
本例子参考了http://hereson.javaeye.com/blog/193390。
具体看代码中的注释吧。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3    
 4    <mx:Script>
 5        <![CDATA[
 6            import mx.controls.Alert;
 7            //声明一个URLLoader类的对象
 8            private var urlloader:URLLoader = new URLLoader();
 9            
10            private function callphp():void{
11                //给这个URLLoader对象注册监听器,当load完成时触发
12                urlloader.addEventListener(Event.COMPLETE, changeValue);
13                //以一个URLRequest实例为参数,调用load方法。把要访问的php页面网址作为URLRequest的参数
14                urlloader.load(new URLRequest('http://192.168.100.100/flex/testphp/getIp.php'));
15            }
16            
17            //urlloader注册的事件触发后执行此函数
18            private function changeValue(event:Event):void{
19                //把数据显示出来,urlloader.data里面存放的是数据
20                text.text = urlloader.data;
21            }
22        ]]>
23    </mx:Script>
24    <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
25    <mx:Button x="296" y="132" label="get" click="callphp()"/>
26</mx:Application>
上面的代码是不传递参数的情况。如果需要传递参数,则需这样写:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3    
 4    <mx:Script>
 5        <![CDATA[
 6            import mx.controls.Alert;
 7            //声明一个URLLoader类的对象
 8            private var urlloader:URLLoader = new URLLoader();
 9            //把要访问的php页面网址作为URLRequest的参数
10            private var urlrequest:URLRequest = new URLRequest('http://192.168.100.100/flex/testphp/getIp.php');
11            //声明一个URLVariables,这个变量是用来存储参数的
12            private var values:URLVariables = new URLVariables();
13            
14            private function callphp():void{
15                //给这个URLLoader对象注册监听器,当load完成时触发
16                urlloader.addEventListener(Event.COMPLETE, changeValue);
17                //类似html表单中的method属性,将传输方式设为POST
18                urlrequest.method = URLRequestMethod.POST;
19                //添加参数,这个message是自定义的,即为参数名字
20                values.message="hello flex!";
21                //把参数绑定到urlrequest上 
22                urlrequest.data = values;
23                //以一个URLRequest实例为参数,调用load方法。
24                urlloader.load(urlrequest);
25            }
26            
27            //urlloader注册的事件触发后执行此函数
28            private function changeValue(event:Event):void{
29                //把数据显示出来,urlloader.data里面存放的是数据
30                text.text = urlloader.data;
31            }
32        ]]>
33    </mx:Script>
34    <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
35    <mx:Button x="296" y="132" label="get" click="callphp()"/>
36</mx:Application>
37

附php端代码:
1  <?
2  $para   =   $_POST [ ' message ' ];
3  echo   $para . '    ' . $_SERVER [ ' REMOTE_ADDR ' ];
4  ?>

转载于:https://www.cnblogs.com/Cnol/archive/2009/08/27/1555385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值