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

标签 javascript jquery ajax xmlhttprequest

我正在尝试在 XMLHttpRequest 中设置超时,但它显示无效状态错误,这是代码

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 };

enter image description here

我查看了以下链接 link1 link2 link3并特别将 xhr.timeout 保持在 xhr.openxhr.send

之间

这个我也试过

xhr.onreadystatechange = function () {
  if(xhr.readyState == 1 ) {
    xhr.timeout = 5000;
  }
};

但是没有运气

最佳答案

xhr.open方法后添加xhr.timeout

关于javascript - xhr 超时在 ie11 中给出无效状态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43361213/

相关文章:

javascript - 单击进入 Bootstrap-Table 时获取单元格的值

javascript - 如何为 JavaScript 重定向和关闭添加延迟?

javascript - TR 折叠时 <TR> 标签中的 jQuery next 不起作用

javascript - PHP服务器只接受部分图片,跨域

javascript - 页面加载时的 Jquery 设置页面状态

javascript - header 部分超过 10240 字节(可能未正确终止)

javascript - 动态创建的文本输入不会调整宽度

javascript - jQuery:是否存在由数据插入 HTML 元素(比如 div)触发的事件?

php - 使用ajax php mysql实现分页搜索

javascript - 谷歌登录弹出窗口不会持续很长时间或显示 "the requested action is invalid"