42

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().ajax.url(ajaxURL).load();

By default it shows an alert and I can change that to throw a javascript error with the following setting:

$.fn.dataTable.ext.errMode = 'throw';

But with this, I just have an error logged to the console and I'm not sure how to catch that thrown error so I still can't provide my own error handler.

There is also an error event listed in the documentation, but that doesn't seem to get triggered, so the following never alerts.

$table.on( 'error', function () { alert( 'error' );} );

Everything else I've found so far is for the legacy code, such as setting the fnServerData, which I would like to avoid getting into.

Is there a method to set the ajax error callback in the 1.10 API?

1
  • 1
    Since ajax calls are asynchronous, it can bypass exception handling. The error probably needs to get caught in their code. You're right, the lack of an error handler is a strange omission. This is why I don't use DataTables's ajax methods. I just use the jQuery 'ajax', which does have success and error handlers, and redraw the table from scratch. Commented Jan 20, 2015 at 16:35

5 Answers 5

60

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I updated to 1.10.6 and now I am able to show a custom alert message.
Thanks. I added this to my main master blade. Every pages doesn't require datatable so I needed to wrap it with this if($.fn.dataTable !== undefined){}
25

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);
        }
    },
});

1 Comment

any way to set a global error function?
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": {
          ....

1 Comment

also don't forget to add $.fn.dataTable.ext.errMode = 'none'; before event to turn default error alert off
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"){
        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;
      }                           
    } 
  }

1 Comment

I'm using DataTables 1.13.x but dataSrc is not called or fired when there is an error (for example HTTP 500). It will be working only success response (HTTP 200 or something like that).
1
"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;
      }                           
    } 
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.