With jQuery 1.9, they changed $.parseJSON to behave in the same way as JSON.parse. The most notable change that has caused me headaches is that now empty strings are considered invalid JSON, and cause jQuery to throw an error.
In the codebase who's version of jQuery I am upgrading, there are many places where we are making ajax requests with dataType: 'json', and in some of those instances there are situations where the server may respond with an empty string. This wasn't an issue in our current version of jQuery, but I'm trying to upgrade, and its causing problems.
It would be a daunting task to change all of the instances of $.ajax with dataType: 'json', in the codebase just to work around the error that $.parseJSON throws, so I am looking for an alternative way of working around this issue. Would something like $.ajaxPrefilter work?
The best solution I could think of is to intercept data that comes in via all ajax calls with dataType: 'json' and pre-process it it using something similar to jQuery's own suggested workaround, to make it work without having to change each ajax call individually.
You can use ajaxSettings() to override the default converters:
$.ajaxSettings({converters: { "text json": yourSafeParseJSON } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With