json data displays in console not in browser – javascript-Collection of common programming errors
I have some json data I retrieve from php code by a jquery $.post()
request. The format is json. But the data is not processed on my brower when the callback function is called
$.post(url_send, data_to_send, function(callback_data){
var info = callback_data.info;
console.log(callback_data.info);
...
});
callback_data = {"success":[{"id":some_number}],"info":"some_string"}
To debug, I noticed that : if I put in this code console.log(callback_data);
the right json data displays in my console, but if I let console.log(callback_data.info)
, the console displays 'undefined'
. Could someone explain that?
Edit : in my php I have $reply = array(‘success’ => $ids,’info’ => $info); echo json_encode($reply);
-
callback_data will be in string format you have to convert it to json using eval to use it as an object.. once you do the eval you should have callback_data.info something like this
$.post(url_send, data_to_send, function(callback_data){ callback_data = eval('(' + callback_data + ')'); console.log(callback_data.info); ... });
-
The json returned by your php page probably doesn’t contain a field named “info”. So, when you try
callback_data.info
, since there’s no info, it’s undefined. -
The field is information, it shoud be: callback_data.information
-
There is not attribute in bject named info in ocallback_data but information use information
Live Demo
information = callback_data.information
Originally posted 2013-11-09 21:17:23.