javascript 如何捕获涉及 XmlHttpRequest 的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26756752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch errors involving XmlHttpRequest?
提问by Hit-or-miss
I want to catch all errors with the help of TRY {} CATCH(){} when I send data to a server via XMLHttpRequest.
当我通过 XMLHttpRequest 将数据发送到服务器时,我想在 TRY {} CATCH(){} 的帮助下捕获所有错误。
How can I receive all errors, such as net::ERR_INTERNET_DISCONNECTED
, etc. ?
我如何才能收到所有错误,例如net::ERR_INTERNET_DISCONNECTED
等?
采纳答案by soundhiraraj
Refer this,
参考这个,
function createXMLHttpRequestObject()
{
// xmlHttp will store the reference to the XMLHttpRequest object
var xmlHttp;
// try to instantiate the native XMLHttpRequest object
try
{
// create an XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch(e) { }
}
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
回答by Francois Dermu
Try catches didn't work for me. I personally ended up testing for response == "" and status == 0.
尝试捕获对我不起作用。我个人最终测试了 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;
回答by disp_name
You should put all the statements which you think that will cause an exception in a try block. After that you can give several catch statements - each one for one exception. In last you can give finally as well - this statement will be executed after Try block regardless of whether or not exception was thrown or caught.
您应该将所有您认为会导致异常的语句放在 try 块中。之后,您可以给出多个 catch 语句——每个语句对应一个异常。最后,您也可以给出 finally - 无论是否抛出或捕获异常,该语句都将在 Try 块之后执行。
Syntax can be like this:
语法可以是这样的:
try{
try_statements
}
[catch (exception_var_2) { catch_statements_1 }]
[catch (exception_var_2) { catch_statements_2 }]
...
[catch (exception_var_2) { catch_statements_N }]
[finally { finally_statements }]
Example:
例子:
try {
myroutine(); // may throw three exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
You can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
您可以在此处阅读更多信息:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch