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.
Is there a problem in Firefox and Chrome whereby these values are not supported?
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?
Share Improve this question edited Aug 9, 2021 at 9:44 MeanwhileInHell asked Aug 1, 2011 at 12:41 MeanwhileInHellMeanwhileInHell 7,05318 gold badges66 silver badges118 bronze badges 1- 1 sometimes it works, sometime it don't, in the same firefox, sometimes httpRequest.DONE works but not XMLHttpRequest.DONE, weird – Puggan Se Commented Jul 6, 2016 at 13:34
3 Answers
Reset to default 7You should be checking readyState against one of the numeric values, 4 in your case.
Some browser does not know XMLHttpRequest.DONE
property, so you should check it as follows before first 'if':
var DONE = (typeof XMLHttpRequest.DONE !== 'undefined') ? XMLHttpRequest.DONE : 4;
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;
}
}
};