key:value
JSON オブジェクトのペアを反復して動的テーブルを作成しようとしています。
現時点では、オブジェクトにペアが 1 つしかないため、テーブルには 2 つのセルを含むテーブル行が 1 つしかないはずですが、テキストとして含まれる<td>
2 つのセルを含む 30 行以上を取得"undefined"
しています。何が原因なのかわからない。
HTML:
<div class="posts">
<table><h2>Posts from My Leaders</h2>
<tr>
<th></th>
<th></th>
</tr>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
jQuery:
var gsd = $.post('forumquery_test.php', {'postarray' : postarray}, function(result, success) {
console.log(result);
var json = $.parseJSON(result); //[{"Username":"stan","Post_txt":"Hi, this is Stan"}]
console.log(json[0].Post_txt); //Hi, this is Stan
console.log(json[0].Username); //stan
$.each(result, function(index, value) {
var username = value.Username;
var posttxt = value.Post_txt;
var newrow = '<tr><td>'+username+'</td><td>'+posttxt+'</td></tr>';
$('.posts table').append(newrow);
})
})
結果:
Username Post
undefined undefined
undefined undefined
etc., etc.,