last day (14 days later) » 

9:18 PM
-1
Q: How can I override strict JSON parsing in jQuery's .ajax() method?

alexwAs of jQuery 1.9, if you specify dataType: json in a call to .ajax but the response body cannot be successfully parsed as JSON, the request will silently fail: The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty re...

 
@adeneo Unfortunately, .ajax does not provide any sort of built-in graceful fallback mechanism (i.e., I prefer valid JSON, but if that is not available, I'll take what I can get).
 
use plain text and parse it yourself. Or, fix the server. An endpoint returning either json or text without input from client depicting which should be returned is broken.
 
@KevinB you're seriously downvoting this question just because of how you think it should be done? This is a perfectly legitimate question.
 
I don't disagree that it is a legitimate question, if i did i would have cast a close vote.
 
The reason I posted this - and answered my own question - was because this is a significant problem that I'm sure a lot of people would like to know how to solve. Just saying "fix the server" is not acceptable in a lot of use cases.
 
9:18 PM
the dataType property accepts multiple values separated by spaces as of version 1.5, and hints at the idea of jsonp xml first trying to convert jsonp to xml, and then if failing, converts the jsonp to text and then xml. that if failing portion leads me to believe that there may be some other way you could solve this without the need of a custom converter, but the documentation is a bit unclear on this topic
 
Right, I tried this.
jQuery is still going to run the response through its built-in parsing routine and throw an exception.
 
Right. but, in what scenario would you have a request that returns either "success" or a json response? i'm curious because i doubt that such a scenario exists. Surely if the request was successful, it would either always return json or always return "success", what would make the request do one or the other?
and if that situation never occurs... what use does this QA pair have?
 
well, I have this scenario
I have an "ajax debugging mode", where I need an HTML error page when it is enabled, and a JSON error message when it is disabled
so, I need to tell the server what type of response I expect
and this applies to both success and error responses.
 
with jquery, that's pretty easily solvable if you have access to the server logic, but if not, then there could be another solution
 
yeah, the solution I posted
 
9:23 PM
For example, JSON.parse('"success"') is successful
 
I don't see the problem with a custom converter
the thing is, my "successful" posts might respond with an empty response body
and an empty response body is interpreted as bad JSON
so if I have my ajax debugging mode enabled, and I submit a successful request, even if I get bad JSON back, I still need it to be interpreted as success.
 
"" is also valid json. what needs to happen is if the response isn't json, it could be easily converted to valid json
at which point the result would be in the success callback, whether the result is an object or a string
wouldn't matter
(which could be done in your converter)
 
sure
 
but... all jquery ajax requests set this header that you can use to differentiate between an ajax request and a browser request
 
that's why I suggest that the reader plug in whatever handling they want
 
9:26 PM
if it's an ajax request, you can always return a json response
no need for a ajax debugging mode, you could essentially always be in that mode and let the server-side error handler decide what format the error message should be in
 
well, I don't always want a json response for ajax requests
the server-side handler chooses the format based on the Accepts header
(well, mostly)
actually now that I figured out how to use dataType effectively, I could probably rely completely on the Accepts header
 

last day (14 days later) »