最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem with XMLHttpRequest.DONE values supported in FFChrome - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 7

You 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;
        }
    }
};
发布评论

评论列表(0)

  1. 暂无评论