参数"真"在xmlHtt prequest。开()方法 [英] parameter "true" in xmlHttpRequest .open() method
问题描述
这是我读MDN参考,它说
From the reference I read in MDN, it says
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.
我使用AJAX已经但是后来我有点困惑,当我读到。我认为这个问题可能是我不理解AJAX概念clearly..I知道,当然AJAX不刷新页面,这意味着连接到服务器和响应在后台完全做到了。
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.
但我可以发生根据该参考想像,如果我有一个code像这样在我的JavaScript:
But what I can imagine happening according to that reference is that if I have a code like this in my JavaScript:
//true, therefore process the function while server retrieves url
var xmlResponse;
var url = "http://domain.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);
难道这不是意味着xmlResponse可以在这个意义上,该服务器仍然检索数据是不确定的?可能有人解释一下什么是真正执行的AJAX技术的流程?先谢谢了。
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.
推荐答案
I wrote a more detailed article here, but this is the basic idea.
将其设置为true意味着你在做一个异步请求。这意味着code没有暂停,直到HTTP请求完成。同步调用锁定浏览器,以便没有别的运行。这可能会造成问题,所以人们preFER异步的。
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.
XHR对象更新我们在做什么。它给我们提供了onreadystatechange事件的更新。我们注册了它的功能,所以我们可以跟踪其状态。该onreadystatechange的被称为4倍。每一个不同的状态
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
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
中的数据提供给我们当readyState为4。
The data is available to us when the readystate is 4.
现在在您发布的code,它是检查的完整状态,它可以确保状态为200 [OK]
Now in the code you posted, it is checking for the complete state and it makes sure that the status is 200 [ok]
if(xml_req.readyState == 4 && xml_req.status == 200){
有关xmlResponse的值,如果您尝试使用它在其他地方,在code返回前,将是不确定的。一个例子
The value for xmlResponse will be undefined if you try to use it somewhere else in the code before it is returned. An example
ml_req.send(null);
alert(xmlResponse );
一对XMLHtt prequest文章的第一篇可能是一个良好的阅读你。在xmlhtt $ P $ 苹果第PQ
这篇关于参数"真"在xmlHtt prequest。开()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!