Exploit
Exploit

Reputation: 6386

how to append jquery ajax (json) to table

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

Answers (2)

Ram
Ram

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>");
})  

http://jsfiddle.net/ADvCJ/

Upvotes: 6

StaticVariable
StaticVariable

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

Related Questions