我的页面中有一个datatable,如下所示:
var dataSet = [
{ "id": 1, "name": "Bennett Weimann", "email": "jtremblay@example.com" },
{ "id": 2, "name": "Bennett Weimann", "email": "buckridge.orin@example.com" }
];
$(document).ready(function () {
$('#example').DataTable({
data: dataSet,
columns: [
{ data: 'id', title: 'Id' },
{ data: 'name', title: 'Name' },
{ data: 'email', title: 'Email' },
],
});
});另外,我有一个按钮,它使ajax帖子请求,在回答中我得到一个json。
[{"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
{"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}]如果我试图像下面这样添加响应,就会得到一个错误
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});错误是"DataTables警告:表id=example -请求0行的未知参数'id‘,列0。有关此错误的更多信息,请参见http://datatables.net/tn/4"。
但是,如果我手动复制和粘贴响应,它就没有任何问题。
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add([
{"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
{"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}
]);
table.draw();
});任何帮手都会发现什么会导致这样的错误。
发布于 2022-06-25 08:12:05
我一定很累..。睡眠后,我可以看到我传递的是一个对象,而不是一个数组。我使用php作为后端,如果我创建一个数组,如
$data = array([
"id" => 1,
"name" => "foo",
"email" => "bar"
]);
return $data;通过回应,它就像一种魅力
request.done(function(response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});https://stackoverflow.com/questions/72750565
复制相似问题