Using DataTables 1.10.15 in Server Side mode. I've created a PHP script to provide a JSON response which includes the parameters they mention in the documentation: https://datatables.net/manual/server-side#Returned-data
I want to add my own parameters to the JSON response, e.g.
$response = [
'data' => [ ], // Required by DataTables
'form_errors' => [ ] // Not required by DataTables
];
echo json_encode($response);
The js which I have for the ajax call looks like this:
var myTable = $('#myTable').DataTable( {
"serverSide": true,
"ajax": {
"url" : "/response.php",
"method" : "POST"
},
});
How can I read the ajax response? I've seen in the API that there is a .on('xhr')
method (https://datatables.net/reference/event/xhr) which fires when the ajax request has been completed, e.g.
var myTable = $('#myTable').DataTable( {
"serverSide": true,
"ajax": {
"url" : "/response.php",
"method" : "POST"
},
}).on( 'xhr.dt', function () {
// Read response here?
});
But I cannot find a way to read the ajax response data at that point.
Does anyone know if this is possible?
Old question, but i'll try to answer may need for future, because i have exactly same problem and after looking for their documentation i found drawCallback
.
From your code:
var myTable = $('#myTable').DataTable( {
"serverSide": true,
"ajax": {
"url" : "/response.php",
"method" : "POST"
},
"drawCallback": function (settings) {
// Here the response
var response = settings.json;
console.log(response);
},
});