Firefox cannot parse JSON string in jQuery AJAX response

I am having a strange problem while trying to parse JSON strings in a jQuery AJAX response. Here is my code:

$.ajax({
    type: "POST",
    url: "Save",
    data: {
        expiry: expiry,
        settings: settings
    }
}).done(function (msg) {
    alert(msg);
    var obj = jQuery.parseJSON(msg);
    if (obj.status == "done") {
        window.location = obj.redirect;
    }
});

In IE, Chrome and Safari, I get a JSON string in warning mode, but in Firefox I get

[obj XMLDocument]

in the message.

Here is the FF console:

enter image description here

obj is null , but I see the response JSON string in the console in a text attribute

responses=Object { xml=document, text="{"status":"done","redir...ippetImage\/s\/6abb68"}

Any reason for this behavior?

+5
source share
2 answers

The problem was on the servlet side. I had to set contentType to make it work.

response.setContentType("text/JSON");
+5
source

This is not a valid JSON string. A JSON string for all purposes and goals is just the right part of the job in JS.

eg.

var x = 7;
        ^
var y = [1,2,3];
        ^^^^^^^
var z = {a:'b', c: 'd'};
        ^^^^^^^^^^^^^^^

, ^, , , x/y/z vars JSON.

json- JS,

var x = ...json_string_here...;

JSON. :

var x = responses=Object { .... }

.

+1

All Articles