jQuery parsererror and WCF Data Services
If you’re using WCF Data Services (REST APIs for SharePoint, for example) and you’re getting the mysterious “parsererror” error message from jQuery, chances are you’ll need to modify your scripts according to this bug report.
The root of the error is the occurrence of single quotes within your JSON response. This can be fixed by adding the following snippet of code before you make your AJAX calls:
1 2 3 4 5 6 7 8 9 10 11 |
$.ajaxSetup({ // use custom converter to handle invalid json data // returned by WCF Data Service // fixes invalid escaped single quotes (\') in json data // e.g. { "foo": "bar\'tender" } --> { "foo": "bar'tender" } converters: { "text json": function( textValue ) { return jQuery.parseJSON( textValue.replace(/(^|[^\\])\\'/g, "$1'") ); } } }); |
Oddly, this error seemingly came out of nowhere for me; script was working fine one day and broken the next…
Luckily, the patch in this ticket seems to have fixed it for me.