i’m getting following data from json
Return Data:
{
"items": [{
"name": "abc",
"meta_value": "3020"
}, {
"name": "xyz",
"meta_value": "3020"
}]
}
i’m using this to encode data echo json_encode(array('items' => $data));
and following to decode and print data from json but it is giving error of
“Uncaught TypeError: Cannot read property ‘length’ of undefined”
$.each(data.items, function(i, item) {
$('#dobsondev-ajax-table').append('<tr><td>' + data.name + '</td><td>' + data.name + '</td><td>' + data.meta_value + '</td><td>' + data.name + '</td></tr>');
});
Try this. You can use
item
argument inside the loop in the function or you can also trydata.items[i]
.You can’t call the first item in the array directly with
[0]
. The first thing you have to do is retrieving the items from your returned data, like this:This will give you the two item objects of your returned data. From there you can retrieve the two item objects like this:
Or in a loop: