<CharlieDigital/> Programming, Politics, and uhh…pineapples


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:

    // 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.

Posted by Charles Chen

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.