0

我在 google 上搜索了不同的页面,我发现了问题,但没有找到解决方案。

我创建了一个类,该类称为 WebServiceController:

public class WebServiceController 
{
    private var url:String;
    private var post:Object=new Object();
    private var loader:URLLoader = new URLLoader();
    private var postVariable:String="";
    private var getVariable:String="";
    private var Geladen:Boolean=false;

    public function WebServiceController() 
    {   

    }

    public function postUrlData(u:String,p:Object):String
    {
        url=u;
        post=p;

        var urlReq:URLRequest = new URLRequest (url);


        var i:int=0;
        for(var key:String in post) 
        {
           //trace(key +" = " + post[key]);
           if(i==0)
           {
              // urlVars.key = post[key];
               postVariable=postVariable+""+key+"="+post[key];
           }
           else
           {
               //urlVars.key = post[key];
               postVariable=postVariable+"&"+key+"="+post[key];
           }
           i++;
        }
        //trace(postVariable);
        var urlVars:URLVariables = new URLVariables(postVariable);

        //trace(urlVars);

        // Add the variables to the URLRequest
        urlReq.data = urlVars;
        urlReq.method = URLRequestMethod.POST;


        // Add the URLRequest data to a new Loader
        //loader.load(urlReq);

        // Set a listener function to run when completed
        loader.addEventListener(Event.COMPLETE, onLoaderComplete);

        // Set the loader format to variables and post to the PHP
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.load(urlReq); 

        function onLoaderComplete(event:Event):void
        {
            return loader.data;
        }

    }

现在从我的movieclip我调用下一个函数,但它说未定义:

var wb:WebServiceController = new WebServiceController();
trace(wb.postUrlData(url,post));

我不知道如何解决这个问题。我尝试了不同的东西,但它一直说未定义。

4

1 回答 1

2

URLLoader.load调用是异步执行的,因此当您尝试在实例化您的WebServiceController类的类中跟踪数据时,不会返回数据。

要访问父类中的数据,最好的办法可能是WebServiceController在数据加载后从类中分派一个事件并在父类中捕获它。

网络服务控制器:

  public function postUrlData(u:String, p:Object):void
  {
      url=u;
      post=p;

      var urlReq:URLRequest = new URLRequest (url);


      var i:int=0;
      for(var key:String in post) 
      {
         //trace(key +" = " + post[key]);
         if(i==0)
         {
            // urlVars.key = post[key];
             postVariable=postVariable+""+key+"="+post[key];
         }
         else
         {
             //urlVars.key = post[key];
             postVariable=postVariable+"&"+key+"="+post[key];
         }
         i++;
      }
      //trace(postVariable);
      var urlVars:URLVariables = new URLVariables(postVariable);

      //trace(urlVars);

      // Add the variables to the URLRequest
      urlReq.data = urlVars;
      urlReq.method = URLRequestMethod.POST;


      // Add the URLRequest data to a new Loader
      //loader.load(urlReq);

      // Set a listener function to run when completed
      loader.addEventListener(Event.COMPLETE, onLoaderComplete);

      // Set the loader format to variables and post to the PHP
      loader.dataFormat = URLLoaderDataFormat.VARIABLES;
      loader.load(urlReq); 
  }

  private function onLoaderComplete(event:Event):void
  { 
      // We can pass the event on like this
      dispatchEvent(event); 
  }

父类:

public function initWebServiceController():void 
{
  var wb:WebServiceController = new WebServiceController();
  wb.addEventListener(Event.COMPLETE, onWebServiceControllerDataLoaded);
  wb.postUrlData(url, post);
}

private function onWebServiceControllerDataLoaded(event:Event):void
{
  // The event target is the URLLoader instance. We can 
  // access the loaded data via its data property
  trace(URLLoader(event.target).data);  
}
于 2013-06-30T00:32:01.083 回答