我有以下代码将JSON数据获取到数据表中。直接访问Json URL很好。 JSON已验证。但是,没有任何内容加载到表中。
JS中是否包含任何内容,或者CDN是否包含错误的顺序?根据几个示例,这应该起作用。感谢您的帮助!
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap4.min.css" type="text/css">
<title>Table</title>
</head>
<body>
<table class="table table-hover table-stripe" id="example_table">
<thead>
<tr>
<td>User</td>
<td>Party</td>
<td>Agent</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous" type="text/javascript">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous" type="text/javascript">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous" type="text/javascript">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/dataTables.bootstrap4.min.js" type="text/javascript">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js" type="text/javascript">
</script>
<script type="text/javascript">
var example_table = $('#example_table').DataTable({
ajax: {
url: 'json_user.php', // the url to get data from
dataSrc: function(data) {
return data.data; // returning the source of the data (it requires an array of data)
}
},
columns: [{
data: "USERID" // 1st column will render the "id" from data
}, {
data: "PARTYID" // 2nd column will render the "name" from data
}, {
data: "Agent" // 3rd column will render the "position" from data
}]
});
</script>
</body>
</html>
我的JSON看起来像这样:
{
"data": [{
"PARTYID": "9999",
"USERID": "oaeg",
"REG_DATE": "2017-04-11 10:19:24",
"LAST_DEPOSIT_DATE": null,
"LAST_DEPOSIT_AMOUNT": null,
"LAST_WITHDRAW_DATE": null,
"LAST_WITHDRAW_AMOUNT": null,
"AgentID": null,
"Agent": "John"
}, {
"PARTYID": "1000001",
"USERID": "Master",
"REG_DATE": "2019-03-08 16:03:52",
"LAST_DEPOSIT_DATE": null,
"LAST_DEPOSIT_AMOUNT": null,
"LAST_WITHDRAW_DATE": null,
"LAST_WITHDRAW_AMOUNT": null,
"AgentID": null,
"Agent": null
},
// ...
}]
}
答案 0 :(得分:1)
控制台中的第一个错误:
无法设置未定义的属性'nTf'
是因为页脚中的td
元素比表中其余部分多。修复后,您会收到此错误:
TypeError:h.ajax不是函数
这是因为您使用的是jQuery的“苗条”分支,该分支已删除了很多功能,主要是与动画和AJAX有关。后者是工作所需的。因此,您需要使用完整版的jQuery:
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
解决了这些问题后,假设返回的JSON与预期的一样,您的代码就可以正常工作。