Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.get() Array Returns [object Object]

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?

like image 925
Norbert Avatar asked Mar 14 '26 15:03

Norbert


2 Answers

Try:

alert(n + ': name = ' + val.name + ' time = ' + val.time);
like image 171
Darin Dimitrov Avatar answered Mar 16 '26 05:03

Darin Dimitrov


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

like image 32
Mark Coleman Avatar answered Mar 16 '26 03:03

Mark Coleman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!