我有以下代码:
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.load(myRequest);
writeText(myLoader.data);
var vars:URLVariables = new URLVariables(myLoader.data);
writeText(vars.peerID);
get_peerID.php?
获取显示:
peerID=5a00d01af308bb4261198d92a89b939979e7ea260a3ead7d49a9b6bdd0492b72
但是,writeText(vars.peerID)
总是显示null
。我似乎无法弄清楚为什么。有什么想法吗?
最佳答案
URLLoader类是异步的。引用文档:
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.
因此,vars.peerID 在调用 URLLoader.load 方法后直接工作的唯一方法是您的网络延迟为零并且服务器端处理的执行时间为 0。两者都极不可能。
相反你应该听complete事件。
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);
然后在代码后面的某处,像这样:
public function onComplete(event:Event):void{
writeText(myLoader.data);
var vars:URLVariables = new URLVariables(myLoader.data);
writeText(vars.peerID);
}
关于php - 这个 URLRequest 或 Loader 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5845202/