HttpService与后台

mx:HTTPService的使用非常简单,我写了一个简单的例子,代码如下:

?

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009

               xmlns:s="library://ns.adobe.com/flex/spark

               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">

    <s:layout>

        <s:BasicLayout/>

    </s:layout>

    <fx:Script>

        <![CDATA[

            import mx.controls.Alert;

            import mx.events.FlexEvent;

            import mx.rpc.events.FaultEvent;

            import mx.rpc.events.ResultEvent;

            import mx.utils.UIDUtil;

              

            protected function application1_creationCompleteHandler(event:FlexEvent):void

            {

                service.url = "http://localhost:8080/demo/Test1.action?rnd=" + new Date().toString();

                service.send();

            }

  

  

            protected function service_resultHandler(event:ResultEvent):void

            {

                Alert.show(String(event.result));

            }

  

  

            protected function service_faultHandler(event:FaultEvent):void

            {

                Alert.show("请求异常");

            }

  

        ]]>

    </fx:Script>

    <fx:Declarations>

        <mx:HTTPService id="service" resultFormat="text" result="service_resultHandler(event)" fault="service_faultHandler(event)" />

    </fx:Declarations>

      

</s:Application>

 

通过service.url指定一个请求的地址,我添加了一个动态的参数,可以保证每次请求的都是最新的页面,防止页面被缓存

service.send();方法开始调用这个请求.

result事件是在调用成功后执行,fault事件是在调用失败后执行

resultFormat属性为text的时候,在result事件,可以通过String(event.result)得到请求页面的输出结果

 

你可能感兴趣的:(HttpService与后台)