我正在尝试在 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 };
我查看了以下链接 link1 link2 link3并特别将 xhr.timeout
保持在 xhr.open
和 xhr.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/