Side navigation
#11151 closed enhancement (fixed)
Opened January 10, 2012 04:10PM UTC
Closed January 11, 2012 03:39PM UTC
Last modified February 28, 2013 08:01PM UTC
jQuery AJAX Error Response Body Parsing
Reported by: | mako281@gmail.com | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.7.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
I use jQuery with REST APIs and sometimes errors are reported via HTTP, with codes like 403 to indicate validation errors and whatnot.
As of now, jQuery will not even attempt to parse the response bodies if the status code is in the error code ranges. It would be nice if the response body could be parsed before getting passed off to the handler. (or perhaps if the ajaxConvert
function could be exposed via jqXHR so we have access to that same conversion power in our error handlers)
I also understand one other scenario: an HTTP error occurs, and then attempts to parse are unsuccessful, leading to a parser error. (then jQuery would have 2 different errors it has to choose between) There could probably be different catch
logic for error conditions than for normal conditions, but that would probably just leave an ugly empty catch
block.
If ajaxConvert
is exposed to the jqXHR object, then I could just use ajaxPrefilter
or ajaxSetup
to plant my own error handler that automatically parses the body. I wouldn't even mind hacking on the source some and submitting a pull request for review.
Considering this is an edge case, I wouldn't be in favor of adding bytes for it. You can always call $.parseJSON yourself, which puts you in control of which error you prefer to process.