我试图将JSON
文件数据插入到html表中,但我不知道问题出在哪里。我共享我的JavaScript代码并共享json文件。请看一下代码
javascript
<script>
function display() {
$(function() {
$.getJSON('my.json', function(data) {
$.each(data.courselist , function(i, f) {
var tblRows = "<tr>" + "<td>" + "Name" + "</td>" + "<td>" + f.categoriesId + "</td>" + "</tr>" + "<tr>" + "<td>" + "Info" + "</td>" + "<td>" + f.shortName + "</td>" + "</tr>";
$(tblRows).appendTo("#category tbody");
});
});
});
}
</script>
{"success":true,"courselist":{"categoriesId":[],"shortName":"Geography Environment & Ecology"}}
答案 0 :(得分:0)
您以错误的方式解析JSON,请read this并查看代码段:
var data = {"success":true,"courselist":{"categoriesId":[],"shortName":"Geography Environment & Ecology"}};
$.each(data.courselist , function(key, val) {
var tblRows = "<tr>" + "<td>" + key + "</td>" + "<td>" + val + "</td>" + "</tr>";
//$(tblRows).appendTo("#category tbody");
console.log(tblRows);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>