我正在尝试进行XHR呼叫,但是如果服务器不在线或者在呼叫发起后Internet断开连接,那么我将无法获得状态 Network Log Screenshot
我尝试了onload(),onerror(),onreadystatechange()事件,但均未触发。 这是我的Javascript代码段
function login() {
animateLogo(true);
document.getElementsByTagName('fieldset')[0].setAttribute('disabled', 'true');
var http = new XMLHttpRequest();
var url = "localhost:3000/login";
var params = `email=${email}&password=${password}&remember=${remember}`;
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onload = function () {
animateLogo(false);
// Response handler
}
http.send(params); }
由于未触发任何事件,因此我无法停止动画
答案 0 :(得分:0)
您可以检查请求的状态 (成功= 200,notFound = 404,serverError = 500等。)
const http = new XMLHttpRequest();
const url = "localhost:3000/login";
http.open('POST', url, true);
const params = `email=${email}&password=${password}&remember=${remember}`;
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
// Request finished. Do processing here.
}
// handle other status here
}
http.send(params);