XHR.readyState == 4&&来自chrome扩展的XHR.status == 0

时间:2011-11-22 09:44:45

标签: javascript google-chrome-extension

我很难找到一个bug。考虑一下这个代码(删除业务部分以保护无辜者):

xhr.onreadystatechange = function() {
        if (this.readyState != 4) {
            debug("state was not 4, it was " + this.readyState);
            return;
        }
        debug("state was 4, status is "+this.status);
        if (this.status == 200 && this.responseText) {
            //(blablabla)
            handleSuccess();
            return;
        }
        debug("no response text..., status is "+ this.status);
        handleError();
    };

在控制台中,我最终得到的结果如下:

[DEBUG] state was not 4, it was 1
[DEBUG] state was not 4, it was 2
[DEBUG] state was not 4, it was 3
[DEBUG] state was not 4, it was 3
[DEBUG] state was not 4, it was 3
[DEBUG] state was not 4, it was 3
[DEBUG] state was not 4, it was 3
[DEBUG] state was 4, status is 200
[DEBUG] no response text..., status is 200
[DEBUG] Handling error

[DEBUG] state was not 4, it was 1
[DEBUG] state was 4, status is 0
[DEBUG] no response text..., status is 0

[DEBUG] state was not 4, it was 1
[DEBUG] state was 4, status is 0
[DEBUG] no response text..., status is 0

[DEBUG] state was not 4, it was 1
[DEBUG] state was 4, status is 0
[DEBUG] no response text..., status is 0

为什么我会有这样的结果?

这些是非常意外的,完全随机发生。有些用户看到了,有些则看不到。在扩展安装时很常见,但在浏览器或手动重新加载时可以正常工作。

有一件事减少了频率:使用window.timeout调用此代码。如果我通过直接呼叫(使用分机的其他功能)来呼叫它,它大部分时间都有效(不是每次都有效)。

谢谢你的时间!

0 个答案:

没有答案