ajax 就绪状态 vs 状态==200
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17561463/
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
readyState vs status==200
提问by user2507818
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
Above code is from:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp.
以上代码来自:http: //www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp。
Question:
题:
According to this tutorial:
根据本教程:
readyState: 4: request finished and response is ready
status: 200: "OK"
When readyState is 4 and status is 200, the response is ready:
since when xmlhttp.readyState == 4, response is ready, why do we still need xmlhttp.status == 200? what is the difference between xmlhttp.readyState == 4and xmlhttp.status == 200?
从什么时候开始xmlhttp.readyState == 4,响应已经准备好了,为什么我们还需要xmlhttp.status == 200?之间有什么区别xmlhttp.readyState == 4和xmlhttp.status == 200?
回答by Ray Nicholus
The status of the response, xhr.status, is (generally) used to determine whether the request was successful or not. xhr.readyStateis simply used to determine the state of the request, such as "has not yet been sent" (0), "complete and response received" (4), etc.
响应的状态xhr.status(通常)用于确定请求是否成功。 xhr.readyState简单地用来判断请求的状态,比如“尚未发送”(0)、“完成并收到响应”(4)等。
The server is responsible for providing the status, while the user agent provides the readyState.
服务器负责提供status,而用户代理提供readyState。
回答by Aguardientico
status indicates if server response is ok.
In general words, when you got an status
状态指示服务器响应是否正常。
一般来说,当你获得一个身份时
500 - 599:the server had an error
400 - 499:this is a client error (Ex: 404 page not found)
300 - 399:then exists a redirect
200 - 299:then it is correct and
100 - 199:means information message
500 - 599:服务器出错
400 - 499:这是一个客户端错误(例如:404 页面未找到)
300 - 399:然后存在重定向
200 - 299:那么它是正确的
100 - 199:表示信息消息
Then the status==200 is getting you a message where the server says: 'Hey man I do the work!'
然后 status==200 会向您发送一条消息,其中服务器说:“嘿,伙计,我来做这项工作!”
回答by Anshul
my analogy : An ambulance carrying a patient is going to a hospital.In this scenario family(client) and hospital (server) needs to keep track of two things
我的比喻:一辆载着病人的救护车要去医院。在这种情况下,家庭(客户端)和医院(服务器)需要跟踪两件事
- Ambulance reached hospital successfully.(
readyState) - Health updates of patient during this process.(
status)
- 救护车顺利到达医院。(
readyState) - 在此过程中患者的健康更新。(
status)
If readyState === 4and status === 200, means everything is all right.In the same way ajax call works.
如果readyState === 4和status === 200, 表示一切正常。以同样的方式调用 ajax 工作。
回答by Aung Kyaw Htay
readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:
readyState 保存 XMLHttpRequest 的状态。从 0 到 4 的变化:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready status 200: "OK" 404: Page not found
0:请求未初始化
1:服务器连接建立
2:收到请求
3:处理请求
4:请求完成,响应就绪状态 200:“OK” 404:页面未找到
回答by bfavaretto
Consider you have an error on your server side code. You request the page, and the readyState will be 4 when the server finishes streaming the response, but the status code will be 500 (or 500-something).
考虑您的服务器端代码有错误。您请求页面,当服务器完成流式响应时,readyState 将为 4,但状态代码将为 500(或 500 左右)。
回答by Vivek Mutha
readyStateHolds the status of the XMLHttpRequest.
readyState保存 XMLHttpRequest 的状态。
0: request not initialized
0:请求未初始化
1: server connection established
1:服务器连接建立
2: request received
2:收到请求
3: processing request
3:处理请求
4: request finished and response is ready
4:请求完成,响应准备就绪
statusReturns the status-number of a request
status返回请求的状态号
200: "OK"
200:“好的”
403: "Forbidden"
403:“禁止”
404: "Not Found"
404:“未找到”
for details of response messages visit
有关响应消息的详细信息,请访问
回答by Graphic ReDesign
Illustration: You (client) send a package to your girlfriend (server), but requires her signature (200 - signed or 404 - not signed). You can check the status of your package (0 - at post office, 4 - out for delivery). Once the package leaves the facility, it will be marked out for delivery (4). But if your girlfriend is not at home when the package arrives, it will return to the post office as not signed (404).
插图:您(客户)向您的女朋友(服务器)发送了一个包裹,但需要她的签名(200 - 签名或 404 - 未签名)。您可以检查包裹的状态(0 - 在邮局,4 - 用于交付)。一旦包裹离开设施,它将被标记为交付 (4)。但是如果包裹到达时你的女朋友不在家,它会以未签名的形式返回邮局(404)。
So, it is possible for a status to be received regarding the package (4) and a status of whether delivery was successful (404). With AJAX, a request can be made to a server. Once the requested information has been obtained (this includes the status of the server - whether the data/document was found), then it is sent back to the receiver (client) for analysis, prior to displaying any content.
因此,可以接收关于包裹的状态(4)和递送是否成功的状态(404)。使用 AJAX,可以向服务器发出请求。一旦获得请求的信息(这包括服务器的状态 - 是否找到数据/文档),然后在显示任何内容之前将其发送回接收器(客户端)进行分析。
回答by icedwater
The readyStaterefers to the response of the request - it is 4 when you have finished retrieving the response and there is something to look at. The actual value is still not known, so you need to check the statusthat has been returned.
该readyState指请求的响应-这是4,当你完成检索响应和有东西看。实际值仍然未知,因此您需要检查status已返回的值。
回答by Palak Juneja
onreadystatechange=function() executes only when we get the response from the server. If we get response from the server means our request is finished which is indicated by 4.And 200 tells us its status that is correct. "4: request finished and response is ready status 200" We get status 404 if the page is not found.
onreadystatechange=function() 仅在我们从服务器获得响应时执行。如果我们从服务器得到响应意味着我们的请求已完成,由 4 表示。 200 告诉我们它的状态是正确的。“4:请求已完成且响应已准备好状态 200” 如果未找到页面,我们将获得状态 404。

