Way to handle statusCode in ajax call of DataTable

Way to handle statusCode in ajax call of DataTable

kvn9328kvn9328 Posts: 18Questions: 7Answers: 0
edited November 2014 in Free community support

Hi,

I am using Server side processing Datatable and when my page's session times out, datatable gives an alert message on my page specifying Invalid JSON. This message is because since my session is time out, the status code will be 302 and in the responsde header i will see the location of redirect page. Since Datatable is expecting JSON and the response header is not one , i can understand i see the alert message.

But, what i want to achieve here is to e able to read the status code and redirect to location of redirect page:

What i have tried:

table = $("#pagination").DataTable({
                    "processing":true,
                    "serverSide":true,
                    "stateSave":true,
                    "ordering":false,
                    "searching":false,
                    "ajax":{
                    "url":"ajax-pagination-read.html",
                    "data": function(d){d.Search = $("#search-form").serialize();}},
                    "statusCode":
                    {
                        302: function(){
                        //do appropriate action
                        },
                        
                        200: function(){
                        alert('success');
                        }
                    }}});

Other option that i have tried is:

"complete": function(jqXHR, textStatus)
                    
{alert(jqXHR);
    alert(textStatus);
},

I never hit complete/statusCode events at all after ajax calls from datatables.

@allan,

Could you please let me know, what wrong am i doing here? If possible post a snipped of code which provides an example of how it can achieve what i am trying to do.

Thanks!

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Currently what you would need to do is provide ajax as a function that will make your custom Ajax call. There is an example of that in the ajax documentation (although not using statusCode).

    DataTables doesn't yet support that parameter directly. It is something I'm considering for a future version. The only "tricky" bit is how to pass in the callback function.

    Allan

  • kvn9328kvn9328 Posts: 18Questions: 7Answers: 0

    @allan,

    Thanks for the reply. I can pass ajax as a function. But i am using columnsDef feature to render html for one column. I don't know how i could do that if i pass ajax as a function.
    Would i be able to that even if i pass it as a function?

    Thanks!

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    I don't see columnDefs in the above code, but I don't see any reason why they can't be used while also using ajax as a function.

    Allan

  • onkar.vaidya@ness.comonkar.vaidya@ness.com Posts: 2Questions: 0Answers: 0
    edited December 2014

    Hi Allan

    Can you provide example for the same? Documentation says -

    data : data to send to server - does this mean URL of the server to get data from?
    I think - I dont need settings object as well.

    what I want achieve is ----

    if (xhr.status == "401")  {  
    
       window.location.replace("../login"); 
    
    } 
    

    Assumption:

    a)

    $('#example').dataTable( { 
    
    
      "ajax": function (data, callback, settings) { 
    
    // I write my own jquery here to get data from server 
    // and then check for status code == 401 as above.  
    
        callback(
    
          JSON.parse( localStorage.getItem('dataTablesData') ) 
    
    // Do I really need any callback in my case? 
    
        );
      }
    
    } );
    

    b) Every time I do

    table.ajax.url(url_toget_the_data_from).load() 
    

    thereafter will invoke the above code and log user out when status code is 401.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    I'll try to put an example together when I get back into the office next week, although there is a bit of a queue at the moment, so apologies if it takes longer.

    Allan

  • onkar.vaidya@ness.comonkar.vaidya@ness.com Posts: 2Questions: 0Answers: 0

    Hi Allan

    Can you find some time for this now? It will be real helpful. Thanks.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Sorry - completely bogged down in priority support requests at the moment. I'll try to do so as soon as I can, but up to my eye balls at the moment!

    Allan

This discussion has been closed.