What data is the parameter errorThrown (third parameter of jQuerys error callback function) Is it something, that the server (backend one has written, in my case it is some REST-based services written in Java) has to return, or is it something more general, which is "automatically" returned without one has to add some error-handling in ones code?
3 Answers
Well, from jQuery.ajax()s documentation:
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) ... When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."...
So the errorThrown string coorseponds to the HTTP status response for any failure responses (>399) For a 400, it would be Bad Request, 500: Internal Server Error, etc...
Quote from the documentation:
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions.
So basically the errorThrown parameter has nothing to do with the response sent from the server. It's just the error reason associated to the status code. If your server uses custom error reasons in addition to setting the status code, this parameter might be useful to retrieve it.
Comments
Something to note (I just ran into this) is that errorThrown will be an empty string for requests that were made with HTTP/2.
This is because the property comes from the statustext property of the underlying XHR, and that property does not get populated for HTTP/2 requests.
More info here: Why is the statusText of my XHR empty?