I am trying to pass an array with json in php:
$array = array ($a, $b, $c);
echo json_encode($array);
and to use it in jquery like this:
$(function () {
$.ajax({
url: 'api.php',
data: "",
dataType: 'json',
success: function (data) {
var id = data[3];
var vname = data[1];
$('#description').html("<b>id: </b>" + id + "<b> name: </b>" + vname);
}
});
});
But this prints as "id: [object Object] name: [object Object]" instead of the values I want, I get that..
What am I doing wrong? Thank you for any help..
[object Object]
means that $a
, $b
and $c
are objects. To print out an object in javascript you should use data[index].variable
where variable
is the property of the object. Try to console.log(data)
to see what you get: you should see 3 objects. Expand them, and read the variables inside of them.
Also, since you're array is made of 3 elements, data[3]
should not exists. You should post more PHP code.