XMLHttpRequest

HTTP Status Codes


On receiving a request, a Web server interprets it and generates an HTTP response message. The first line of the message is the status line containing the HTTP version, a numeric status code and its corresponding textual description - a reason phrase:

HTTP/1.1 200 OK

The constituent parts of the status line can be retrieved by analyzing the values of the XHR status and statusText properties:

var xhr=new XMLHttpRequest();
xhr.onreadystatechange=showStatus;
xhr.open("GET", "http://example.com/");
xhr.send();
. . .
function showStatus(event) {
 if(event.target.readyState==4) {
  console.log(event.target.status); // if HTTP message exchange is successful, status returns 200
  console.log(event.target.statusText); // associated textual phrase of 200 code is OK
 }
}

The properties are supported in the COM XMLHTTP implementation, too:

// cscript.exe xhr.js //NoLogo
var xhr=new ActiveXObject("Msxml2.XMLHTTP.6.0");
xhr.onreadystatechange=function() {
 if(xhr.readyState==4) {
  WScript.Echo(xhr.status);
  WScript.Echo(xhr.statusText);
 }
}
xhr.Open("GET", "http://example.com/", true);
xhr.Send();

Main status codes and their respective reason phrases are listed below:

1xx informational codes

100 Continue
101 Switching Protocols

2xx success codes

200 OK
201 Created
202 Accepted
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content

3xx redirection codes

300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
307 Temporary Redirect

4xx client error codes

400 Bad Request
401 Unauthorized
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 Satisfiable
417 Expectation Failed

5xx server error codes

500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported