set an error handler for jquery datatables ajax call
Use the Ajax error function to log errors:
$('#table').DataTable({
ajax: {
dataType: "JSON",
type: "POST",
url: url,
data: [],
async: true,
error: function (xhr, error, code) {
console.log(xhr, code);
}
},
});
I am using Datatables 1.10.19 + Bootstrap and most of the solutions provided (including the accepted one above) did not work. However, I managed to capture the error as follows:
"ajax" : {
"datatype" : "json",
"contentType" : "application/json",
"method" : "GET",
"url" : $url,
"data" : { x : y },
"dataSrc": function (data) {
if (data.result == "OK"){
return data.yourObj;
}else{
// hide processing or any loading modal here
// display error on page or something
console.log("Error: " + parseResultData(data.resultData));
data.yourObj = [] //since datatables will be checking for the object as array
return data.yourObj;
}
}
}
New error event handling has been added in Datatables v1.10.5 (released 10th February 2015).
$.fn.dataTable.ext.errMode = function ( settings, helpPage, message ) {
console.log(message);
};
See docs here:
https://datatables.net/reference/event/error
https://cdn.datatables.net/1.10.5/
Use the event as a custom error handler:
$(document).ready(function () {
$('#myTable').on('error.dt', function (e, settings, techNote, message) {
console.log('An error has been reported by DataTables: ', message);
}).DataTable({
"displayLength": 15,
"ajax": {
....