jQuery.Post() - accessing the returned json object
Hi,
The below code posts a couple of vars to a php file which sends back a json object containing (in the test case) one object called 'faves' with 2 variables (let's say called var1 and var2). I can't seem to access the variables after the ajax call. The result is always an 'undefined' error message. I've tried:
- console.log(data.var1)
- console.log(data.faves.var1)
- console.log(var1)
All to no avail. Can anyone tell me what I'm doing wrong here? I have checked with FireBug that all the data returned is good and well-formed etc.
- $(function() {
$.post("<? echo base_url(); ?>test.php", {
- id: "<?php echo $id; ?>",
- action: "check", code: '' },
function(data){
console.log(data.var1);
}, "json");
});
many thanks,
Mike