Javascript xmlHttpRequest .open() 方法中的参数“true”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6461958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parameter "true" in xmlHttpRequest .open() method
提问by Benny Tjia
From the reference I read in MDN, it says
从我在 MDN 中读到的参考文献中,它说
If TRUE (the default), the execution of the JavaScript function will continue while the response of the server has not yet arrived.
This is the A in AJAX.
如果为 TRUE(默认值),JavaScript 函数将继续执行,而服务器的响应尚未到达。
这是 AJAX 中的 A。
I have been using AJAX but then I was a little confused when I read that. I think the problem may be that I am not understanding AJAX concept clearly. I know of course AJAX does not refresh the page which means the connection to the server and the response are completely done in the background.
我一直在使用 AJAX,但是当我读到它时我有点困惑。我认为问题可能是我没有清楚地理解 AJAX 概念。我当然知道 AJAX 不会刷新页面,这意味着与服务器的连接和响应完全在后台完成。
But what I can imagine happening according to that reference is that if I have a code like this in my JavaScript:
但是根据该参考我可以想象发生的事情是,如果我的 JavaScript 中有这样的代码:
//true, therefore process the function while server retrieves url
var xmlResponse;
var url = "http://example.com/file.xml";
xml_req.open("GET", url, true);
xml_req.onreadystatechange = function() {
if(xml_req.readyState == 4 && xml_req.status == 200) {
if(xml_req.responseText != null)
xmlResponse = xml_req.responseXML; //server response may not yet arrive
else {
alert("failed");
return false;
}
};
xml_req.send(null);
Doesn't that mean xmlResponse could be undefined in the sense that the server is still retrieving the data? Could somebody explain what really is the flow of the execution in AJAX technology? Thanks in advance.
从服务器仍在检索数据的意义上说,这是否意味着 xmlResponse 可能未定义?有人能解释一下 AJAX 技术中的执行流程到底是什么吗?提前致谢。
采纳答案by epascarello
I wrote a more detailed article here, but this is the basic idea.
我在这里写了一篇更详细的文章,但这是基本思想。
Setting it to true means you are making an asynchronous request. That means the code does not pause until the http request is complete. A synchronous call locks up the browser so nothing else runs. That can cause problems, so people prefer asynchronous.
将其设置为 true 意味着您正在发出异步请求。这意味着代码在 http 请求完成之前不会暂停。同步调用会锁定浏览器,因此不会运行其他任何东西。这可能会导致问题,因此人们更喜欢异步。
The XHR object updates us on what it is doing. It gives us the updates with the onreadystatechange event. We register a function with it so we can keep track of its status. The onreadystatechange gets called 4 times. Each with a different state
XHR 对象向我们更新它正在做什么。它通过 onreadystatechange 事件为我们提供更新。我们向它注册一个函数,以便我们可以跟踪它的状态。onreadystatechange 被调用 4 次。每个都有不同的状态
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
The data is available to us when the readystate is 4.
当就绪状态为 4 时,我们可以使用数据。
Now in the code you posted, it is checking for the complete state and it makes sure that the status is 200 [ok]
现在在您发布的代码中,它正在检查完整状态并确保状态为 200 [ok]
if(xml_req.readyState == 4 && xml_req.status == 200){
The value for xmlResponse will be undefined if you try to use it somewhere else in the code before it is returned. An example
如果您在返回之前尝试在代码中的其他地方使用 xmlResponse 的值,则该值将是未定义的。一个例子
ml_req.send(null);
alert(xmlResponse );
One of the very first articles on the XMLHttpRequest article might be a good read for you. Apple Article on xmlhttpreq
XMLHttpRequest 文章中的第一篇文章可能适合您阅读。Apple 关于 xmlhttpreq 的文章
回答by gilly3
The important thing to understand is that your onreadystatechange
handler is not executed immediately. And it is executed more than once. It may be easier to conceptualize, if you break the pieces out into individual functions:
要了解的重要一点是您的onreadystatechange
处理程序不会立即执行。它被执行不止一次。如果将各个部分分解为单独的功能,则可能更容易概念化:
function makeRequest(url)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = receiveResponse;
xhr.send();
}
function receiveResponse(e)
{
if (this.readyState == 4)
{
// xhr.readyState == 4, so we've received the complete server response
if (this.status == 200)
{
// xhr.status == 200, so the response is good
var response = this.responseXML;
...
}
}
}
First, makeRequest
is called and then exits. Then, as soon as we hear anything back from the server, receiveResponse
is called. Each time, we check to see if the response is fully received, and only then do we continue to process that response.
首先,makeRequest
被调用然后退出。然后,一旦我们从服务器听到任何消息,receiveResponse
就会调用。每次,我们都会检查是否完全收到响应,然后才继续处理该响应。