简体   繁体   中英

How do I determine what kind of error ajaxError is handling?

I'm trying to handle AJAX errors in a web application that uses jQuery.

I'm setting up an event handler to deal with AJAX errors like this:

$('body').ajaxError(error_handler);

However, despite the information that's passed to that handler, I'm finding it difficult to determine what sort of error occured.

The two errors I'm interested in identifying are:

  • When the application server is down
  • When I've used $.getJSON(...) and the response isn't parsable as JSON data.

Both of these cause my error handler to fire, but I need to handle each error differently, and don't know how I can do this.

Does anyone have any suggestions?

The XMLHttpRequest is the second argument to the function. You can check the status property to get the HTTP status code. That should tell you if you got a valid response in an unexpected format or if the server is down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM