当我通过 XMLHttpRequest 向服务器发送数据时,我想借助 TRY {} CATCH(){} 捕获所有错误。
如何接收所有的错误,例如net::ERR_INTERNET_DISCONNECTED
等?
最佳答案
try catch 对我不起作用。我个人最终测试了 response == ""和 status == 0。
var req = new XMLHttpRequest();
req.open("post", VALIDATE_URL, true);
req.onreadystatechange = function receiveResponse() {
if (this.readyState == 4) {
if (this.status == 200) {
console.log("We got a response : " + this.response);
} else if (!isValid(this.response) && this.status == 0) {
console.log("The computer appears to be offline.");
}
}
};
req.send(payload);
req = null;
关于javascript - 如何捕获涉及 XmlHttpRequest 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756752/