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.timeout
在xhr.open
和xhr.send
我什至试过这个
xhr.onreadystatechange = function () {
if(xhr.readyState == 1 ) {
xhr.timeout = 5000;
}
};
Run Code Online (Sandbox Code Playgroud)
但没有运气
xhr.timeout
仅当以异步方式调用时才xhr.open()
应设置,否则MSIE
会引发异常INVALID_STATE_ERR
。
示例: xhr.open("POST", url, true);
附:奇怪的是,我在 FF 和 Chrome 中没有看到这个问题。
归档时间: |
|
查看次数: |
1751 次 |
最近记录: |