javascript

Accessing http status code constants


I'm looking for a list of http status codes in Javascript. Are they defined in any implementation?

I've had a look at XMLHttpRequest, but only found readyState constants.

var xhr = new XMLHttpRequest();
console.log(xhr.DONE); //4

I'm looking for something like

console.log(xhr.statusCodes.OK); //200

Which obviously doesn't exist on the xhr object.


Solution

  • Http status codes are maintained by the Internet Assigned Numbers Authority (IANA), whereas readyState is specific to XmlHttpRequest.

    Therefore just go to a reputable source. The wikipedia article should suffice as this is not really a contentious topic - or, as commented, the official list can be found here

    You could also wrap those you are interested in into a javascript object

    var HttpCodes = {
       success : 200,
       notFound : 404
       // etc
    }
    

    usage could then be if(response == HttpCodes.success){...}