I'm trying to break an AJAX call in as many ways as I can to anticipate many specific errors. Using the error callback in jQuery ajax, doesn't give me specific enough information compared to what the browser logs (which I would like to catch). I would receive the same generic information (status of 0 with a statusText of "error") for these two errors from Chrome in the error callback:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'some url' is therefore not allowed access. The response had HTTP status code 503.
GET http://google.com/ net::ERR_INTERNET_DISCONNECTED
Right now my code is similar to the answer in how can i handle multiple ajax errors in jquery . I would like it to address more specific errors, but I suspect it's not possible to catch the specific errors the browser logs.
Is it even possible to catch these errors?
Note: Wrapping the call in a try-catch does not work either. Using vanilla XHR doesn't provide specific error information either.
That answer refers to handling the .status
object, which will only contain a number. If you want to handle error messages, use the .responseText
, and either JSON.parse()
it or use indexOf()
to check on the exceptions you want to use. You can also use the thrownError
string:
$.ajax({
...
error: function(xhr, ajaxOptions, thrownError) {
console.log("thrownError: " + thrownError);
}
});
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.