Flex4 调用后台的方法

Remote.as

 

 

package com.remote
{
 import mx.rpc.AbstractOperation;
 import mx.rpc.events.ResultEvent;
 import mx.rpc.remoting.mxml.RemoteObject;

 public class Remote
 {
  public static const url:String="http://localhost:8080/remote/messagebroker/amf";
  
  public  static function getData(call:Function,method:String):void
  {
   var load:RemoteObject=new RemoteObject();
   load.destination="service";
   load.endpoint=url;
   var mthd:AbstractOperation = load.getOperation(method);
   mthd.service.addEventListener(ResultEvent.RESULT,call);
   mthd.send();
  }
 }
}

 

main.mxml

 

 

<?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">
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 </fx:Declarations>
 <s:Label id="result" x="100" y="100"/>
 <s:Button label="测试" click="remote()" />
 <fx:Script>
  <![CDATA[
   import com.remote.Remote;
   
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;
   public function remote():void
   {
    Remote.getData(showResult,"callback");
   }
   public function showResult(evt:ResultEvent):void
   {
    result.text=evt.result.toString();
   }
  ]]>
 </fx:Script>
</s:Application>

你可能感兴趣的:(function,Flex,application,callback,import,library)