开发者

Whats wrong with this URLRequest or Loader?

I have the following code:

var myRequest:URLRequest = new URLRequest("http://localhost/example.com/scripts/get_peerID.php?peerID=" + myID.text);
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = "URLLoaderDataFormat.VARIABLES";
m开发者_如何转开发yLoader.load(myRequest);
writeText(myLoader.data);
var vars:URLVariables = new URLVariables(myLoader.data);
writeText(vars.peerID);

And the get_peerID.php? Gets displays:

peerID=5a00d01af308bb4261198d92a89b939979e7ea260a3ead7d49a9b6bdd0492b72

However, writeText(vars.peerID) always displays null. I can't seem to figure out why. Any ideas?


The URLLoader class is asynchronous. To quote the docs:

A URLLoader object downloads all of the data from a URL before making it available to code in the applications. It sends out notifications about the progress of the download, which you can monitor through the bytesLoaded and bytesTotal properties, as well as through dispatched events.

So, the only way that vars.peerID will work directly after calling the URLLoader.load method is if your network has zero latency and your server side processing has a 0 execution time. Both, are extremely unlikely.

Instead you should listen to the complete event.

var myRequest:URLRequest = new URLRequest("http://localhost/example.com/scripts/get_peerID.php?peerID=" + myID.text);
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = "URLLoaderDataFormat.VARIABLES";
myLoader.addEventListener(Event.COMPLETE,onComplete);
myLoader.load(myRequest);

then somewhere later in code, something like this:

public function onComplete(event:Event):void{
 writeText(myLoader.data);
 var vars:URLVariables = new URLVariables(myLoader.data);
 writeText(vars.peerID);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜