Javascript xhr.readystate===4 是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30522565/
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
What is meaning of xhr.readystate===4
提问by truongnguyen1912
回答by truongnguyen1912
An Ajax http request has 5 states as your reference documents:
Ajax http 请求有 5 个状态作为您的参考文档:
0 UNSENT open() has not been called yet.
1 OPENED send() has been called.
2 HEADERS_RECEIVED send() has been called, and headers and status are available.
3 LOADING Downloading; responseText holds partial data.
4 DONE The operation is complete.
State 4 means that the request had been sent, the server had finished returning the response and the browser had finished downloading the response content. So, it is right to say that the AJAX call has completed.
状态 4 表示请求已发送,服务器已完成返回响应,浏览器已完成响应内容的下载。因此,可以说 AJAX 调用已经完成。
回答by Rahul Tripathi
Yes, it is correct.xhr.readstate===4means request finished and response is ready. You can refer thisfor details.
是的,它是正确的。xhr.readstate===4表示请求已完成,响应已准备就绪。你可以参考这个了解详情。
Here is small example:
这是一个小例子:
xmlhttp.open("GET", "test.txt", true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send(null);
The above script makes a GET request for the relative url "text.txt" (relative to the calling page) It provides the function, which checks the readyState property each time it's called and when it has the value 4 - meaning the load is complete, it displays the responseText to the user with an alert.
上面的脚本对相对 url “text.txt”(相对于调用页面)发出 GET 请求它提供了函数,它在每次调用时检查 readyState 属性,当它具有值 4 时 - 意味着加载完成,它会向用户显示带有警报的 responseText。

