Way to handle statusCode in ajax call of DataTable
Way to handle statusCode in ajax call of DataTable
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.
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
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 theajax
documentation (although not usingstatusCode
).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
@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!
I don't see
columnDefs
in the above code, but I don't see any reason why they can't be used while also usingajax
as a function.Allan
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 ----
Assumption:
a)
b) Every time I do
thereafter will invoke the above code and log user out when status code is 401.
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
Hi Allan
Can you find some time for this now? It will be real helpful. Thanks.
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