Datatables ajax call error handle

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,
    //disable order and search on column
    columnDefs: [
         {
             targets: [4,5],
             //set priority to column, so when resize the browser window this botton stay on the screen because have max priority
             responsivePriority: 1,
             orderable: false,
             searchable: false,
         }
     ],
     //fix problem with responsive table
     "autoWidth": false,
         "ajax":{ 
             "url":"table",
             //Check if there was an error on the query like all other ajax request
             "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,
    //disable order and search on column
    columnDefs: [
         {
             targets: [4,5],
             //set priority to column, so when resize the browser window this botton stay on the screen because have max priority
             responsivePriority: 1,
             orderable: false,
             searchable: false,
         }
     ],
     //fix problem with responsive table
     "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.

+4

Source: https://habr.com/ru/post/1629323/


All Articles