xhr 超时在 ie11 中给出无效状态错误

Ais*_*ngh 5 javascript ajax jquery xmlhttprequest

我试图在 XMLHttpRequest 中设置超时但它显示invalid state error,这是代码

function get(url, options) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();

    // headers
    if (options && options.headers) {
      for (let header in options.headers) {
        if (options.headers.hasOwnProperty(header)) {
          xhr.setRequestHeader(header, options.headers[header]);
        }
      }
    }

    xhr.open('GET', url);
    // FIXME: Why is IE11 failing on "xhr.timeout?
    // xhr.timeout = 10000;

    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        try {
          const data = JSON.parse(xhr.responseText);
          resolve(data);
        } catch (ex) {
          reject({
            status: this.status,
            statusText: xhr.statusText
          });
        }
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };

    xhr.ontimeout = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };

    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };

    xhr.send();
  });
}

export default { get };
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我查看了以下链接link1 link2 link3并专门保存xhr.timeoutxhr.openxhr.send

我什至试过这个

xhr.onreadystatechange = function () {
  if(xhr.readyState == 1 ) {
    xhr.timeout = 5000;
  }
};
Run Code Online (Sandbox Code Playgroud)

但没有运气

Nai*_*der 7

xhr.timeoutxhr.open方法后添加。


tro*_*jan 1

xhr.timeout仅当以异步方式调用时才xhr.open()设置,否则MSIE会引发异常INVALID_STATE_ERR

示例: xhr.open("POST", url, true);

附:奇怪的是,我在 FF 和 Chrome 中没有看到这个问题。