1

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?

dom_beau
  • 2,437
  • 3
  • 30
  • 59
  • What is `malformed JSON` ? Everything looks good and it executes as expected then what is the concern ? – Rayon Feb 10 '16 at 03:57
  • JSON seems alright to me. What is the issue exactly? – pratikpawar Feb 10 '16 at 03:57
  • 1
    If temperature and humidity are updated correctly, then what is the issue? Where are you getting "malformed JSON"? – zero298 Feb 10 '16 at 03:58
  • Actually the only issue is that the Firefox debugger tells me that the JSON is malformed. THat is why I wrote "I don't understand" ;-) – dom_beau Feb 10 '16 at 04:51

1 Answers1

4

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.

Community
  • 1
  • 1
phenxd
  • 689
  • 4
  • 17