关于xmlhttp.status == 0的问题

Ajax中,XMLHttpRequest对象的status属性一般用来返回服务器的HTTP状态码。status为200表示“成功”,status为404代表“页面未找到”。很多书上都是这样写的,一点也不错。

  但是,有时候,特别是刚开始学Ajax的时候,可能会将代码直接在本地运行。这样就出现问题了。 如果在本地运行(如:C:ajaxhelloworld.htm),那么status属性不管是在“成功”还是“页面未找到”的情况下,都返回的是0 ,而 不是200和404。这个时候如果还用if(xmlHttp.status==200)来判断运行,则会毫无结果。如果要在本地测试,最好写成 if(xmlHttp.status==200 || xmlHttp.status==0)的形式来判断。

  就像我前面所说的,这并不是说很多书或文章里写错了。因为这些文章里都写的是从服务器返回状态,而并没有说从本地返回的情况,应该算是漏掉了吧。

下面把STATUS各个值所代表的状态列出来:

XMLHTTP中

xmlHttp.status=0
xmlHttp.status=200
xmlHttp.status=500
等等;
对应的HTTP的状态,见下表:

status状态值
长整形标准http状态码,定义如下: Number Description
100 Continue
101 Switching protocols
200 OK
201 Created
202 Accepted
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
307 Temporary Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Suitable
417 Expectation Failed
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
原文地址:https://www.cnblogs.com/mguo/p/2955341.html