http状态码

http状态码

XMLHttpRequest.status属性返回一个整数表示服务器响应的http状态码。

XMLHttpRequest.statusText属性返回一个字符串,包含整个状态信息。

状态码 状态信息 意义
200 OK 访问正常
301 Moved Permanently 永久移动
302 Moved temporarily 暂时移动
304 Not Modified 未修改
307 Temporary Redirect 暂时重定向
401 Unauthorized 未授权
403 Forbidden 禁止访问
404 Not Found 未发现指定网址
500 Internal Server Error 服务器发生错误

也就是说:只有2XX和304表示服务器返回是正常状态。

完整状态码:

https://www.runoob.com/http/http-status-codes.html

判断服务器返回是否正常:

1
2
3
4
5
6
7
8
if (xhr.readyState === 4) {
if ( (xhr.status >= 200 && xhr.status < 300)
|| (xhr.status === 304) ) {
// 处理服务器的返回数据
} else {
// 出错
}
}

评论