【发布时间】:2020-03-17 23:26:27
【问题描述】:
我正在尝试在数据表上显示一些数据。从 Ajax 请求中调用数据,响应为 Json 数组,如下所示:
[{"item": "one", "price": 22, "status": "free"}, {"item": "two", "price": 15, "status": "taken"}]
这是我的要求:
<script>
$(document).ready(function() {
$('mytable').DataTable( {
"ajax": "myurl",
"dataType": 'json',
"dataSrc": '',
"columns": [
{"data": "item" },
{"data": "price" },
{"data": "status"},
]
} );
} );
</script>
这是我的 HTML:
<table id="mytable" class="display" style="width:100%">
<thead>
<tr>
<th>ITEM</th>
<th>PRICE</th>
<th>STATUS</th>
</tr>
</thead>
</table>
问题是数据没有显示在表格上。 我认为当 Datatables 尝试处理我的 JSON 数据时会出现问题,因为我在控制台上收到此错误:
Uncaught TypeError: Cannot read property 'length' of undefined
【问题讨论】:
-
是不是你的table不显示数据的问题?
-
是的,很抱歉,我现在编辑了我的帖子
-
您的表格 id 是“开放的”,您的 JS 正在尝试查找
$('mytable') -
刚刚创建问题时出错,已更正
标签: javascript jquery ajax datatables