Reputation: 6386
i have a ajax call that retreives data and its success portion looks like this:
success: function(data)
{
$("table.table").append("<tr><td>" + data.member_id + "</td><td>" + data.comment + "</td></tr>");
}
the data variable is holding this data
[{"member_id":"2","comment":"kkk"},{"member_id":"1","comment":"this is admin 2"},{"member_id":"2","comment":"kkk"},{"member_id":"1","comment":"this is admin"}]
but the problem im getting is that the table td's contain undefined text. how do i fix this?
the hardcoded table looks like this:
<table class="table"></table>
Upvotes: 1
Views: 14884
Reputation: 144679
You should loop through the array, can use $.each
utility function:
$.each(json, function(i, data){
$("table.table").append("<tr><td>" + data.member_id + "</td><td>" + data.comment + "</td></tr>");
})
Upvotes: 6
Reputation: 5283
You can use this
$.each(data, function(i,row){
$("table.table").append("<tr><td>"+row['member_id']+"</td><td>"+row['comment']+"</td></tr>");
})
Upvotes: 0