Skip to content Skip to sidebar Skip to footer

Set An Error Handler For Jquery Datatables Ajax Call

I'm trying to have a custom error handler when something goes wrong (i.e. the server does not respond) on the ajax call for loading new data into my datatable. $table.DataTable().a

Solution 1:

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/errorhttps://cdn.datatables.net/1.10.5/

Solution 2:

Alternatively use error function in ajax to log errors

$('#table').DataTable({
        ajax: {
           dataType: "JSON",  
            type: "POST",
            url: url,
            data: [],
            async: true,
            error: function(xhr, error, code)
            {
                console.log(xhr);
                console.log(code);
            }
        },...

Solution 3:

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": {
          ....

Solution 4:

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"){
        returndata.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 arrayreturndata.yourObj;
      }                           
    } 
  }

Post a Comment for "Set An Error Handler For Jquery Datatables Ajax Call"