Search code examples
javascriptjqueryjsongetjson

Malformed JSON response


I'm getting a malformed JSON response from an AJAX $.getJSON() request. I don't understand the problem.

Here is the request code:

var myfunc = function(){
    $.getJSON( "/", {"data": ""}, function( data, status ){
        var values = data;

        $("#temperature").html( values.temperature.toFixed(1).toString() );
        $("#humidity").html( values.humidity.toFixed(0).toString() );
    });
});

and here is the JSON data received (extracted via Firefox debugger):

{
    "temperature": 17.799999237060547,
    "humidity": 35.900001525878906,
    "failed": false
}

I cannot see what is malformed here. And the code works. DOM elements id="temperature" and id="humidity" are updated correctly.

I got exactly the same result using $.get() with JSON.parse().

Does anybody have an idea how to solve the problem?


Solution

  • My guess is that the json data you are receiving over the network is malformed, but, it is successfully converted to an object anyway.

    getJSON automatically applies JSON.parse(..) on the received data.

    Try using the 'network' listener tab on Google Chrome to see exactly the response you are receiving BEFORE it is parsed. There might be a missing " or something like that.

    If you have access to the server code, you could also try logging the response in there.

    edit: you might be interested by this link Might have to do with some server config.
    Mimetype is also mentionned in this link.