I am using datatables with ajax call, and I would like to add an error descriptor and redirect to 500 pages if there is an error, So far I have this table:
licenseTable = $('#licensesTable').DataTable({
responsive: true,
columnDefs: [
{
targets: [4,5],
responsivePriority: 1,
orderable: false,
searchable: false,
}
],
"autoWidth": false,
"ajax":{
"url":"table",
"dataSrc": function ( json ) {
if (json.success){
return json.result.data;
}else{
notifyMessage(json.result, 'error');
return "";
}
},
},
"columns": [
{ "data": "user" },
{ "data": "startDate",
"render": function (data) {
return (moment(data).format("DD/MM/YYYY"));
}
},
{ "data": "endDate",
"render": function (data) {
return (moment(data).format("DD/MM/YYYY"));
}
},
{ "data": "counter" },
{ data:null, render: function ( data, type, row ) {
return '<button type="button" class="btn btn-primary" id="updadteLicense" data-toggle="modal"'
+'data-target="#updateLicenseModal">Update</button>'
}
},
{ data:null, render: function ( data, type, row ) {
return '<button type="button" class="btn btn-danger" id="deleteLicense" data-toggle="modal"'
+'data-target="#deleteLicenseModal">Delete</button>'
}
}
],
});
So far, so good, but if I add "error": window.location.href = "/ATS/500", it no longer works. I read the manual and maybe I need to use fnServerData because it allows error handling. But how can I convert my code to fnServerData? I tried without success:
licenseTable = $('#licensesTable').DataTable({
responsive: true,
columnDefs: [
{
targets: [4,5],
responsivePriority: 1,
orderable: false,
searchable: false,
}
],
"autoWidth": false,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "table",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax({
"url": sSource,
});
$.getJSON( sSource, aoData, function (json) {
if (json.success){
json=json.result.data;
}else{
notifyMessage(json.result, 'error');
json="";
}
fnCallback(json)
} );
},
It shows my lines, but pagination, ordering and searching do not work. Without a server side, can I somehow use the erro descriptor?
UPDATE:
"url": "table",
"dataSrc": function ( json ) {
if (json.success){
return json.result.data;
}else{
notifyMessage(json.result, 'error');
return "";
}
},
"error": function (xhr, error, thrown) {
window.location.href = "/ATS/500";
}
It works, but when should it go in the error bar? If I use the wrong URL, it goes to dataSrc anyway.