这是我的 JSON
{
"version": "5.2",
"user_type": "online",
"user":
[
{
"name": "John",
"id": 50
},
{
"name": "Mark",
"id": 57
}
]
}
将上述 JSON 转换为 HTML 的 javascript
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://PATH/user.json",
dataType: 'json',
type: 'get',
cache:false,
success: function(data){
/*console.log(data);*/
var event_data = '';
$.each(data, function(index, value){
/*console.log(value);*/
event_data += '<tr>';
event_data += '<td>'+value.name+'</td>';
event_data += '<td>'+value.id+'</td>';
event_data += '<tr>';
});
$("#list_table_json").append(event_data);
},
error: function(d){
/*console.log("error");*/
alert("404. Please wait until the File is Loaded.");
}
});
});
</script>
表的 HTML 代码:
<table class="table table-responsive table-hover table-bordered" id="list_table_json">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
</tr>
</thead>
</table>
在控制台中没有收到任何错误
我在表中得到的输出显示未定义。如何将数据推送到json?
原文由 deepak murthy 发布,翻译遵循 CC BY-SA 4.0 许可协议
你的循环应该像
$.each(data.user, function(index, value){});
并关闭</tr>
最后