Reputation:
I am returning json from server but it's not available in my success callback in ajax.
$.ajax({
url:'myData.php',
type:'post',
success:function(data){
console.log(data.name); // This should print my name
}
});
In php I am using this
$data=array('id' => 1, 'name' => 'john');
echo json_encode($data);
What am I missing ?
Upvotes: 2
Views: 231
Reputation: 21466
Either
Tell jQuery that you are expecting JSON data back. See dataType on http://api.jquery.com/jQuery.ajax/
or
Return a header so that jQuery can detect the response as being JSON:
header('Content-type: application/json');
Upvotes: 3