最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to catch errors involving XmlHttpRequest? - Stack Overflow

programmeradmin3浏览0评论

I want to catch all errors with the help of TRY {} CATCH(){} when I send data to a server via XMLHttpRequest.

How can I receive all errors, such as net::ERR_INTERNET_DISCONNECTED, etc. ?

I want to catch all errors with the help of TRY {} CATCH(){} when I send data to a server via XMLHttpRequest.

How can I receive all errors, such as net::ERR_INTERNET_DISCONNECTED, etc. ?

Share Improve this question edited Nov 5, 2014 at 12:13 Paul 27.4k13 gold badges89 silver badges126 bronze badges asked Nov 5, 2014 at 12:02 Hit-or-missHit-or-miss 5231 gold badge6 silver badges17 bronze badges 2
  • 1 Are you using any javascript frameworks such as jQuery or MooTools? If not, maybe consider it as they have success / error handling built into their AJAX functions as well as taking care of cross browser issues etc. – WizzHead Commented Nov 5, 2014 at 12:08
  • 3 Unfortunately, I don't like any frameworks. I wanna use pure JS) – Hit-or-miss Commented Nov 5, 2014 at 12:17
Add a ment  | 

3 Answers 3

Reset to default 6

Try catches didn't work for me. I personally ended up testing for response == "" and 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 puter appears to be offline.");
                }
            }
        };
        req.send(payload);
        req = null;

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.

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/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

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;
}
发布评论

评论列表(0)

  1. 暂无评论