[英]How to send an HTTP request to a PHP page and get the echo'd response in AS3?
what I want to do is send an HTTP request to a PHP page, get what is being echo'd and return it. 我要做的是将HTTP请求发送到PHP页面,获取正在回显的内容并返回它。
So something like this 所以像这样
PHP Page: PHP页面:
<?php
echo "test";
The AS3 Code: AS3代码:
function openRequest(url) {
open_page (url);
return response_from_page;
}
So I can use the function in something like this in AS3 所以我可以在AS3中以类似方式使用该函数
if (openRequest(php_page.php) == "test") {
//do
}
How can I do this? 我怎样才能做到这一点?
I hope I managed to explain the concept. 我希望我能解释这个概念。
Thanks. 谢谢。
As akmozo said in a comment you can use URLLoader
to perform the HTTP request and read the response after the COMPLETE
event. 正如akmozo在评论中所说,您可以使用
URLLoader
执行HTTP请求并在COMPLETE
事件之后读取响应。
However, you can't do this exactly the way you say. 但是,您不能完全按照您所说的做。 ActionScript cannot wait for an asynchronous action (like an HTTP request) before continuing execution, so a function cannot wait before returning a value.
ActionScript在继续执行之前不能等待异步操作(例如HTTP请求),因此函数不能在返回值之前等待。 In other words
openRequest
can open the HTTP request, but it cannot wait for the response before returning and exiting the function. 换句话说,
openRequest
可以打开HTTP请求,但是不能在返回和退出函数之前等待响应。
Instead, you can use callbacks to sequence your code: 相反,您可以使用回调对代码进行排序:
function openRequest(url:String, then:Function):void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, complete);
urlLoader.loader(new URLRequest(url));
function complete(e:Event):void {
urlLoader.removeEventListener(Event.COMPLETE, complete); // cleanup
then(urlLoader.data);
}
}
Now you can write your code like this: 现在您可以像这样编写代码:
openRequest("php_page.php", function(response) {
if (response === "test") {
// do something
}
});
Note that any code after openRequest
will get executed immediately, not after the response is received. 请注意,
openRequest
之后的任何代码openRequest
将立即执行, 而不是在收到响应之后。 Anything you want to happen after the response is received must be placed inside the callback. 您希望在收到响应后发生的任何事情都必须放在回调中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.