test.php includes this:
echo json_encode( array(
array("name"=>"John","time"=>"2pm"),
array("name"=>"2","time"=>"1242pm"),
array("name"=>"J231ohn","time"=>"2p213m"),
));
jQuery:
$.get("test.php", function(data) {
$.each(data, function(n, val) {
alert(n + ': ' + val)
});
}, "json");
This is the result:
0: [object Object]
1: [object Object]
2: [object Object]
What am I doing wrong?
Try:
alert(n + ': name = ' + val.name + ' time = ' + val.time);
I dont know php but my guess is you need to do this instead, as each val is a json object.
$.get("test.php", function(data) {
$.each(data, function(n, val) {
alert(n + ': ' + val.name + ' ' + val.time)
});
}, "json");
jsfiddle example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With