【发布时间】:2016-10-01 09:28:20
【问题描述】:
我正在做一个项目,我需要将数据发布到从返回 JSON 的查询中接收到的表中。
我在这里查看了其他几篇关于接收 JSON 数据并将其附加到表中的帖子,但他们都没有真正完成我需要的任务。
提及我是 JQUERY 的新手可能是个好主意,但我对需要发生的事情有基本的了解。
我的代码看起来像这样,请注意我使用假 API 来提供 Chuck Norris 的笑话,但最终它会提供大量用于存储文本块的部分。
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<script>
$.getJSON('http://api.icndb.com/jokes/random', function(json_data){
var table_obj = $('table');
$.each(json_data, function(index, item){
var table_row = $('<tr>', {id: item.id});
var table_cell = $('<td>', {html: item.data});
table_row.append(table_cell);
table_obj.append(table_row);
})
})
</script>
<table border=1>
<tbody id="table">
</tbody>
</table>
貌似查询成功,可以查看对象,但是好像没有创建表。
对此的任何建议将不胜感激。
【问题讨论】:
-
检查 API 响应的格式:
{"type":"success","value":{"id":72,"joke":"How much wood would a woodchuck chuck if a woodchuck could Chuck Norris? All of it.","categories":[]}}。包含“笑话”的属性名称是什么?