1
想設置超時在XMLHttpRequest的,但它顯示invalid state error
,這裏的代碼XHR超時給人無效狀態誤差在IE11
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 };
我看看下面的鏈接link1link2link3特別保持xhr.timeout
xhr.open
和xhr.send
我甚至試過這個
xhr.onreadystatechange = function() {
if(xhr.readyState == 1) {
xhr.timeout = 5000;
}
};
,但沒有運氣
關於您的FIXME評論:[@Mozila](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout)是一個注意它。 –