WebServer is nginx, which directly tells me that they have configured the nginx 404 error page. Although a 404 page can be successfully returned for a resource that does not exist in the request, the return status code is 200.
404. html
This is 404 page.
Request a page that does not exist:
Ajax Code:
The code is as follows: |
Copy code |
$. Ajax ({ Url: "does_not_exist.html ", Success: function (Response, textStatus ){ Console. log (textStatus + ":" + Response ); }, Error: function (XMLHttpRequest, textStatus, errorThrown ){ Console. log ([XMLHttpRequest, textStatus, errorThrown]. join (",")); } }); |
Execution result:
Go to the Object to see details:
The requested page is not found and the 404 page information is returned, but the status code is still 200. Therefore, jQuery directly calls the success callback instead of the error callback function.
There should be a problem with configuration writing, so I opened nginx. conf and found that their configuration was written like this:
Error_page 404 =/404.html;
So I read the official documentation and changed the above expression:
Error_page 404/404 .html;
Then restart
D: ginx-1.5.11> nginx.exe-s reload
Try again:
Let's take a look at the ajax request:
Obviously, compared to the previous one, the status code 404 is returned, which is displayed in console. log below:
[Object Object], error, Not Found
Then click to enter the Object to see:
The status value is 404. Both the 404 page and the 404 status code are returned, so that ajax requests can determine the page request status based on the status code for error handling.