I have a datatable in my page initiated like below:
var dataSet = [
{ "id": 1, "name": "Bennett Weimann", "email": "[email protected]" },
{ "id": 2, "name": "Bennett Weimann", "email": "[email protected]" }
];
$(document).ready(function () {
$('#example').DataTable({
data: dataSet,
columns: [
{ data: 'id', title: 'Id' },
{ data: 'name', title: 'Name' },
{ data: 'email', title: 'Email' },
],
});
});
Additionally I have a button which makes an AJAX post request, in the answer I get a JSON
[{"id":1,"name":"Bennett Weimann","email":"[email protected]"},
{"id":2,"name":"Bennett Weimann","email":"[email protected]"}]
If I try to add the response like below, I get an error
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});
Error is
"DataTables warning: table id=example - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"
However if I copy and paste the response manually, it has no issue whatsoever.
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add([
{"id":1,"name":"Bennett Weimann","email":"[email protected]"},
{"id":2,"name":"Bennett Weimann","email":"[email protected]"}
]);
table.draw();
});
Any help appreciated what could cause such error?
I must have been tired.. after a sleep I could see that I was passing an object instead of an array. I am using php as a backend, if I create an array like
$data = array([
"id" => 1,
"name" => "foo",
"email" => "bar"
]);
return $data;
and passing the response it works like charm
request.done(function(response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With