网络请求错误状态码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络请求错误状态码相关的知识,希望对你有一定的参考价值。

参考技术A https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
摘抄其中重点的部分

This class of status codes indicates the action requested by the client was received, understood, accepted, and processed successfully.[2]

Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request, the response will contain an entity describing or containing the result of the action.[7]

The request has been fulfilled, resulting in the creation of a new resource.[8]

The request has been accepted for processing, but the processing has not been completed. The request might or might not be eventually acted upon, and may be disallowed when processing occurs

This class of status code indicates the client must take additional action to complete the request. Many of these status codes are used in URL redirection.[2]

A user agent may carry out the additional action with no user interaction only if the method used in the second request is GET or HEAD. A user agent may automatically redirect a request. A user agent should detect and intervene to prevent cyclical redirects.[18]

Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation). For example, this code could be used to present multiple video format options, to list files with different filename extensions, or to suggest word-sense disambiguation.[19]

This and all future requests should be directed to the given URI.[20]

The requested resource is available only through a proxy, the address for which is provided in the response. Many HTTP clients (such as Mozilla[26] and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons.[27]

In this case, the request should be repeated with another URI; however, future requests should still use the original URI. In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. For example, a POST request should be repeated using another POST request.[29]

The request and all future requests should be repeated using another URI. 307 and 308 parallel the behaviors of 302 and 301, but do not allow the HTTP method to change. So, for example, submitting a form to a permanently redirected resource may continue smoothly.[30]

The 4xx class of status codes is intended for situations in which the client seems to have erred. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents should display any included entity to the user.[31]

The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing).[32]

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.[33] ####401 semantically means "unauthenticated",[34] i.e. the user does not have the necessary credentials.
Note: Some sites issue HTTP 401 when an IP address is banned from the website (usually the website domain) and that specific address is refused permission to access a website.

The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource.

The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.[36]

A request method is not supported for the requested resource; for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource.

The server failed to fulfill an apparently valid request.[55]

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and indicate whether it is a temporary or permanent condition. Likewise, user agents should display any included entity to the user. These response codes are applicable to any request method.[56]

A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.[57]

The server either does not recognize the request method, or it lacks the ability to fulfill the request. Usually this implies future availability (e.g., a new feature of a web-service API).[58]

The server was acting as a gateway or proxy and received an invalid response from the upstream server.[59]

The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.[60]

The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.[61]

The server does not support the HTTP protocol version used in the request.[62]

Ajax---错误处理

http状态码:
1、网络畅通,服务器端能接收到请求,服务器返回的结果不是预期结果。
可以判断服务器端返回的状态码,分别进行处理。xhr.status获取http状态码

2、网络畅通,服务器端没有接收到请求,返回404状态码
检查请求地址是否错误。

3、网络畅通,服务器端能接收请求,服务器端返回500状态码。
服务器端错误,找后端程序员进行沟通。

4、网络中断,请求无法发送到服务器端。
会触发xhr对象下面的onerror事件,在onerror事件处理函数中对错误进行处理。

Ajax状态码:表示Ajax请求的过程状态码,ajax对象返回的
Http状态码:表示请求的处理结果 是服务器端返回的

ajax错误处理

.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <title>07Ajax错误处理.html</title>
 6 </head>
 7 <body>
 8     <button id=\'btn\'>发送Ajax请求</button>
 9     <script type="text/javascript">
10         var btn=document.getElementById(\'btn\');
11         btn.onclick=function(){
12             var xhr=new XMLHttpRequest();
13             xhr.open(\'get\',\'http://localhost:3000/error\');
14             xhr.send();
15             xhr.onload=function(){
16                 //xhr.status 获取http状态码
17                 console.log(xhr.responseText)
18                 if(xhr.status==400){
19                     alert("请求出错!")
20                 }
21             }
22             xhr.onerror=function(){
23                 alert(\'网络中断,无法发送Ajax请求\')
24             }
25         }
26     </script>
27 </body>
28 </html>

app.js

app.get(\'/error\',(req,res)=>{
    res.status(400).send(\'not ok\');
})

有网情况:

 无网情况:

以上是关于网络请求错误状态码的主要内容,如果未能解决你的问题,请参考以下文章

图解HTTP网络

常见常用的网络状态码

Ajax---错误处理

02.计算机网络-HTTP状态码和Header请求体

http网络协议 学习摘要

常见网络错误