2

Server returns this JSON document:

{
"username-found": true,
"question-required": true
}

Which successfully passes JSONLint's validity check.

In web browser:

$.post('my_url', {"post":"data"}, function(data) {
  data = $.parseJSON(data);
});

The code runs and successfully parses the JSON document in Opera 12 browser, however in Firefox 16, JavaScript error occurs and says "not well-formed".

JQuery is of version 1.7.2.

I cannot see what I did wrong there, do you know?

Edit:

Does it have anything to do with the way server returns the JSON? Here it is:

return new StreamingResolution("text", new StringReader(json.toString()));

uggestion, I might have found the cause. When I did alert(data), Firefox tells me that data is an object, Opera tells me that data is the JSON string.

Peter O.
  • 28,965
  • 14
  • 72
  • 87

3 Answers3

2

Solution 1 (Client) - Set DataType in jQuery Request

I think the internals are a bit different in that specific browser version (because jQuery tries to detect the dataType automatically and is doing the parsing internally in the case of a JSON response) and JSON is automatically encoded in FF and not in Opera?

Try to add the dataType so jQuery will handle this (I would prefer that):

$.post('my_url', {"post":"data"}, function(data) {
    // data should be an json object here
}, 'json');

It's just a guess.

OR Solution 2 (Server) - Send MIME type

You could also send a correct MIME type from the server so you don't have to set the dataType on the client. Its up to you but I think that would be the correct solution.

Regarding this answer it should be application/json.

Reference

How is the dataType detected automatically in jQuery?

Default: Intelligent Guess (xml, json, script, or html) The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

Source: http://api.jquery.com/jQuery.ajax/

Community
  • 1
  • 1
Marc
  • 6,535
  • 9
  • 43
  • 75
  • 1
    That's not how you pass the data type, you have to pass it as fourth argument to the function. The second argument is only the data that is supposed to be sent to the server. http://api.jquery.com/jQuery.post/ – Felix Kling Oct 26 '12 at 00:27
  • 1
    Uups, thank you, its a $.post and not an $.ajax call, you're right! I change it. – Marc Oct 26 '12 at 00:28
  • Thank you. I think it is likely the cause of my problem. Firefox appears to automatically parses `data` into object, however Opera leaves it in string form. –  Oct 26 '12 at 00:29
  • 1
    I don't know what exactly jQuery is doing when it has to detect the dataType so always when you have problems just tell jQuery what you will get from the server and you should be fine. – Marc Oct 26 '12 at 00:30
  • I'm getting JSON text using Google's GSON in the form of string. This text parsed properly on **JSONLint** where as it is getting failed when I do `$.parseJSON('')` What care should be taken for it? – Sriram Oct 30 '12 at 15:37
1

You can directly use the data object directly ..

No need to use $.parseJSON();

Sushanth --
  • 53,795
  • 7
  • 57
  • 95
  • Thank you, I just figured that out. Opera leaves `data` in string, however Firefox automatically parses it. Which browser is more correct in this scenario? –  Oct 26 '12 at 00:31
0

You also have a $.getJSON shortcut method in jQuery. Maybe jQuery automatically uses the best configuration for this case and maybe start working

Here is the $.getJSON documentation

edrian
  • 4,377
  • 5
  • 26
  • 34