Problem with XMLHttpRequest.DONE values supported in FF / Chrome

Viewed 10745

I have a snippet of Javascript that I need to debug:

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
    if (this.readyState === XMLHttpRequest.DONE) {
        if (this.status === 200) {
            success = true;
        }
    }
};

Stepping through on Chrome and Firefox, I have found that the first "if" is failing. I can see that this.readyState is set to 1, which judging by the W3C spec should mean OPENED. However XMLHttpRequest.DONE shows as undefined rather than 4 in Firebug.

http://www.w3.org/TR/XMLHttpRequest/#states

Is there a problem in Firefox and Chrome whereby these values are not supported?

3 Answers

You can use the DONE value that the httpRequest variable already has:

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
    if (this.readyState === this.DONE) {
        if (this.status === 200) {
            success = true;
        }
    }
};
Related