0

If I handle jQuery AJAX errors using fail(), I can use textStatus to provide a nice error message to the user:

$.get("/some_script.php")
.done(function() 
{ 
  // It works!
})
.fail(function(jqXHR, textStatus, errorThrown)
{
  switch(textStatus)
  {
    case 'timeout' :
      alert("Timeout");
    break;
    case 'parsererror':
      alert("Corrupted data")
    break;
    case 'error':
    case 'abort':
    default:
      alert("Unknown error");
    break;
  }
});

But when I use the ajaxError() global error handler, textStatus is not available:

$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) 
{ 
  ...
});

I checked the event, jqXHR and ajaxSettings objects to see if textStatus is hiding inside them, but had no luck. thrownError doesn't help me also.

All I want is to make ajaxError() tell me what kind of error occurred: timeout, parsererror, error or abort, just like fail() does.

How can I do that? Thanks.

Marcovecchio
  • 1,322
  • 9
  • 19
  • 1
    *"All I want is to make ajaxError() tell me what kind of error occurred: timeout, parsererror, error or abort, just like fail() does."* isn't that in the `thrownError` param, not `textStatus`? – Kevin B Sep 18 '15 at 20:59
  • 1
    In any event, you can get the status code if available through the jqXHR, which would handle any http-based errors, otherwise thrownError is all you've got. – Kevin B Sep 18 '15 at 21:01
  • @KevinB [is right](http://jsfiddle.net/72xtstkk/) - if you inspect the console you'll see `thrownError` returns what you are looking for. Also, `jqXHR.statusText`. – wahwahwah Sep 18 '15 at 21:02
  • @KevinB, no. thrownError gives me HTTP error text, if available. If the connection had a timeout, thrownError doesn't tell me that. If I use $.getJSON for example, and the result is not JSON parseable, textStatus gives me parsererror, it's WAY better than anything else I ever used. – Marcovecchio Sep 18 '15 at 21:05
  • that makes no sense. I think you've got them swapped. – Kevin B Sep 18 '15 at 21:05
  • possible duplicate of [jquery how to get the status message returned by a ajax call of type post?](http://stackoverflow.com/questions/8422932/jquery-how-to-get-the-status-message-returned-by-a-ajax-call-of-type-post) – Zakaria Acharki Sep 18 '15 at 21:06
  • There's nothing more available to you than what is given to you through the arguments. You can get the status code, and the text status, which will be "timeout" for timeouts. http://jsfiddle.net/72xtstkk/5/ – Kevin B Sep 18 '15 at 21:14
  • @KevinB: but fail() does tell me when there is a parse error or timeout. Did you try that? I will create a jsfiddle as soon as I can. – Marcovecchio Sep 18 '15 at 21:17
  • Sorry, i updated my comment. it DOES give you a timeout message on timeouts. I can't test parseerrors in jsfiddle. See latest jsfiddle in above comment. – Kevin B Sep 18 '15 at 21:18
  • Here it is with invalid json. it doesn't say parseerror, but it's pretty damn close. http://jsfiddle.net/72xtstkk/8/ `SyntaxError: Unexpected token N(…)` – Kevin B Sep 18 '15 at 21:21
  • @KevinB: thanks for that, and it can serve as a workaround if there's no other way, but `fail()` provides a much better and precise information on what happened. – Marcovecchio Sep 18 '15 at 21:27
  • Then use .fail. unfortunately you're bound by the methods you use. An ugly workaround would be to create a wrapper for all ajax requests that attaches your own .fail to each and every one of them. – Kevin B Sep 18 '15 at 21:28

0 Answers0