XMLHttpRequest在Firefox中失败

时间:2019-07-16 10:49:01

标签: javascript json ajax firefox xmlhttprequest

以下代码在Chrome中输出success,在Firefox中输出failure 2

let request = new XMLHttpRequest();
request.open('GET', 'json/foo.json', true);
request.onload = function () {
    if (this.status >= 200 && this.status < 400) {
        console.log('success');
    } else {
        console.log('failure 1');
    }
};
request.onerror = function () {
    console.log('failure 2');
};
request.send();

我完全不知道为什么。该请求不会显示在“网络”标签中。我在控制台上也没有收到任何错误或警告。记录错误会产生以下内容,这似乎无济于事:

{
    bubbles: false​
    cancelBubble: false​
    cancelable: false​
    composed: false​
    currentTarget: null​
    defaultPrevented: false​
    eventPhase: 0​
    explicitOriginalTarget: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }​
    isTrusted: true​
    lengthComputable: false​
    loaded: 0​
    originalTarget: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }​
    returnValue: true​
    srcElement: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }​
    target: XMLHttpRequest { readyState: 4, timeout: 0, withCredentials: false, … }​
    timeStamp: 880​
    total: 0​
    type: "error"
    ​<get isTrusted()>: function isTrusted()
    <prototype>: ProgressEventPrototype { lengthComputable: Getter, loaded: Getter, total: Getter, … }
}

我尝试使用request.overrideMimeType('application/json');并获得了相同的结果,因此它不应与MIME类型相关。

我应该注意,该代码在本地(即使在Firefox中也可以)运行,但不能在服务器上运行。

0 个答案:

没有答案