JavaScript How to refresh DataTables from within ajax/error callback? datatables refresh from ajax,refresh datatable with new data,refresh

I'm extending the DataTables defaults like so:

$.extend(true, $.fn.dataTable.defaults, {
    lengthChange: false,
    deferRender: true,
    displayLength: 25,
    stateSave: false,
    serverSide: true,
    processing: true,
    ajax: {
        type: 'POST',
        error: function($xhr) {
            if($xhr.status === 401) {
                wxu.openLoginBox(function(data) {
                    // HELP: how can I get the DataTables object from this context?
                });
            } else {
                wxu.notify({'text': "Could not load list", 'cssClass': 'error', timeout: 0});
            }
        }
    }
});

Sometimes a user will get logged out and then when they try changing pages or sorting, it just says "Processing" forever. I can catch this by looking for a 401 error response (this is what my app sends when you've been timed out), but then I don't know how to "refresh" dataTables to make the "Processing" message go away so that you can resume using the app.

Please note that I'm extending the defaults here in a .js file -- I don't know what element DataTables is going to be bound to at this point.

How can I "fix" dataTables from inside the ajax/error callback?

Answer:1

NOTES

You should not override ajax.error since this property is used internally by DataTables, the same applies to ajax.success.

SOLUTION

You can add event handler for xhr event to handle Ajax errors (json === null).

// Prevent alert message from being displayed
$.fn.dataTable.ext.errMode = 'none';

// Use delegated event handler
// to handle Ajax request completion event
$(document.body).on('xhr.dt', function (e, settings, json, xhr){
   // If there is an Ajax error and status code is 401
   if(json === null && xhr.status === 401){         
      var api = new $.fn.dataTable.Api(settings);

      console.log('Session expired');
/*     
      wxu.openLoginBox(function(data){
          api.ajax.reload();
      });
*/

   } else {
      console.log('Could not load list');   
/*       
       wxu.notify({'text': "Could not load list", 'cssClass': 'error', timeout: 0});
*/       
   }
});

DEMO

See this jsFiddle for code and demonstration.

Answer:2

I trying to implement keyup function for dynamically added input fields, but it is not working html code <table border="1" id="table"> <colgroup> <col width="100"/> <col width="...

I trying to implement keyup function for dynamically added input fields, but it is not working html code <table border="1" id="table"> <colgroup> <col width="100"/> <col width="...

This answer by Varinder works well but I would like to get the element width which is set in pixels and increase it by 10%. $(window).load(function() { $(".ml").each(function() { // this is ...

This answer by Varinder works well but I would like to get the element width which is set in pixels and increase it by 10%. $(window).load(function() { $(".ml").each(function() { // this is ...

  1. increase element width javascript
  2. increase element width css
  3. increase element width jquery
  4. increase element width
  5. increase element width using javascript

I am working on a project in which I want to play different types of news channels live on my website Here is my code in which I am trying to play the channels <div class="video_box"> <...

I am working on a project in which I want to play different types of news channels live on my website Here is my code in which I am trying to play the channels <div class="video_box"> <...

  1. live channel streaming services
  2. live channel streaming free
  3. live channel streaming reddit
  4. live channel streaming golf
  5. live channel streaming app
  6. live channel streaming india
  7. live channel streaming online
  8. live streaming channel 5

I have this HTML code with pre-written message. My goal is to highlight text between [quote] [/quote] in a yellow background once I focus/click on the text area. <textarea> This is a test ...

I have this HTML code with pre-written message. My goal is to highlight text between [quote] [/quote] in a yellow background once I focus/click on the text area. <textarea> This is a test ...

  1. javascript find and highlight text on page
  2. javascript window.find highlight all
  3. javascript find and highlight text
  4. javascript find string and highlight
  5. javascript find on page highlight
  6. find and highlight javascript