admin管理员组文章数量:1391947
Am trying to set timeout in XMLHttpRequest but it shows invalid state error
, here's the code
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 };
I had a look at following links link1 link2 link3 and specifically kept xhr.timeout
between xhr.open
and xhr.send
I even tried this
xhr.onreadystatechange = function () {
if(xhr.readyState == 1 ) {
xhr.timeout = 5000;
}
};
But no luck
Am trying to set timeout in XMLHttpRequest but it shows invalid state error
, here's the code
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 };
I had a look at following links link1 link2 link3 and specifically kept xhr.timeout
between xhr.open
and xhr.send
I even tried this
xhr.onreadystatechange = function () {
if(xhr.readyState == 1 ) {
xhr.timeout = 5000;
}
};
But no luck
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Apr 12, 2017 at 5:46 Aishwat SinghAishwat Singh 4,4793 gold badges29 silver badges49 bronze badges 1- About your FIXME ment: @Mozila is a note about it. – Bob S Commented Jun 14, 2017 at 15:53
2 Answers
Reset to default 7Add xhr.timeout
after the xhr.open
method.
You should set xhr.timeout
only when you call xhr.open()
in asynchronous mode, otherwise MSIE
will rise an exception INVALID_STATE_ERR
.
Example: xhr.open("POST", url, true);
ps. Strangely, i don't see this issue in FF & Chrome.
本文标签: javascriptxhr timeout gives invalid state error in ie11Stack Overflow
版权声明:本文标题:javascript - xhr timeout gives invalid state error in ie11 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735493a2622302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论