I have ‘little’ problem with javascript numbers and NaN.
I’m retrieving one single number via ajax POST method in JSON format.
$.post('test.php?a=b', {w: w}, function(json) {
desktop.height = desktop.height - json.height * 2;
});
When I try to multiply it, it gives me NaN.
desktop.height = desktop.height - json.height * 2;
NaN
JSON IS an object, right? So when I enter this into firebug/chrome console, it gives me int 0, as expected.
var a = {}; a.t = 0; a.t * 2;
0
Even if I enter this into a console, it still gives me expected 0 int.
var a = {}; a.t = "0"; a.t * 2;
0
My question is, what’s going on and how do I fix it?
If the
Content-Typeheader is not set toapplication/jsonfrom server and still If you want jQuery to parse the json String into JS object automatically then you have to adddataTypejsonin$.postcall.or you have to parse that youself using
$.parseJSONlike this