Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery JSON response always triggers a ParseError

I am trying to preform some basic operations with jQuery and JSON. Presently having difficulty with jQuery accepting JSON response from my play framework application. Below is a simplified version of the code that still produces the error.

$.ajax({
    type: 'POST',
    url: "@{FrontEnd.isUsernameAvailable()}",
    data: "name=thisnameisavailable",
    cache: false,
    success: function(data) {
        console.log("Success... ");
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("Error... " + textStatus + "        " + errorThrown);
    },
    dataType: 'json'
});

The error callback is always triggered. It displays

Error... parsererror jQuery15001997238997904205_1298484897373 was not called

Inspecting the returned JSON through Firebug shows no errors and various JSON lint tools also validate. Changing dataType to "text" makes success be called. But I am trying to use the isUsernameAvailable call as part of jQuery validation plugin so I need it to return valid JSON.

like image 616
Chesrae Avatar asked Feb 23 '11 18:02

Chesrae


People also ask

What does the JSON parse () method do when we initiate an Ajax request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

What is Parsererror in Ajax?

The reason why this parsererror message occurs is that when you simply return a string or another value, it is not really Json , so the parser fails when parsing it. So if you remove the dataType: json property, it will not try to parse it as Json .

What is a parse error in JSON?

parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.


3 Answers

Maybe I'm misunderstanding, but couldn't you set the dataType to text and JSON.parse() the returned data?

success: function(data) {
    data = JSON.parse(data);
    // process data
},

Edited to add generally agreed upon solution (previously a comment only):

I just took a look at api.jquery.com/jQuery.ajax and it looks like with jQuery 1.5 you can do a type conversion of sorts. "multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType." Maybe you can try "text json".

like image 200
ggutenberg Avatar answered Nov 02 '22 06:11

ggutenberg


I got the same error as soon as I upgraded to jQuery 1.5. It turns out that my problem is because I'm also using the jquery validation plugin, which is not compatible with jQuery 1.5. If I remove the jquery validation plugin, $.ajax() with dataType json works fine.

More information about the jquery validation plugin incompatibility here: http://bugs.jquery.com/ticket/8118

like image 12
Johnny Oshika Avatar answered Nov 02 '22 05:11

Johnny Oshika


I also got "parsererror jQueryNNNN_NNN was not called" (using jsonp and jQuery 1.7.2) The reason was that one of the values in the returned json structure contained newlines. Hope this helps someone.

like image 3
Robert Avatar answered Nov 02 '22 06:11

Robert