[英]How to abort XMLHttpRequest if the connection timeout?
我遇到XMLHttpRequest的问题。 如果没有Internet连接,则我的请求没有超时,因此冻结了浏览器。 这是请求代码:
var jsonData = new XMLHttpRequest();
jsonData.open('GET', urltest, false);
jsonData.Timeout = 100;
jsonData.send(null);
if(jsonData.status == 200) {
alert(jsonData.statusText);
}else{
alert(jsonData.statusText);
}
如果服务器没有响应,如何中止请求?
我对您的代码进行了一些更改,并留下了一些注释,在其中进行了更改以解释发生了什么。 希望对您有所帮助!
var jsonData = new XMLHttpRequest();
jsonData.open('GET', urltest);/* I removed the false, because it kept the browser
waiting for the page to load to continue, and it didn't let it do other things
while it was still loading.*/
jsonData.Timeout = 100;
jsonData.send();
jsonData.onreadystatechange = function() {/*Each time readyState changes value,
run this code.*/
if (jsonData.readyState == 4) {/*When readyState is equal to 4,
the page has fully loaded.
Only run the code when it's 4,
not when it's still loading.*/
if (jsonData.status == 200) {
alert(jsonData.statusText);
} else {
alert(jsonData.statusText);
}
// More things to do when the page has fully loaded...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.