How to do error handling on ajax call
How to do error handling on ajax call
Hi All,
This is my first post. I gone through all the example and implemented Data Table at my work. My website suppose to authenticate the user and then allow to see datatable. I am using server side processing so if user is not authorized then he should be redirected to login page. I am looking for solution like if ajax request failed due to unauthorized access redirect it to login page.
I am looking some thing like below
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '../examples_support/json_source.txt',
"onfailure": doSomething
} );
} );
[/code]
One solution is send incorrect sEcho value which will cause error at front end but I want to handle this error.
Looking forward for your response.
This is my first post. I gone through all the example and implemented Data Table at my work. My website suppose to authenticate the user and then allow to see datatable. I am using server side processing so if user is not authorized then he should be redirected to login page. I am looking for solution like if ajax request failed due to unauthorized access redirect it to login page.
I am looking some thing like below
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '../examples_support/json_source.txt',
"onfailure": doSomething
} );
} );
[/code]
One solution is send incorrect sEcho value which will cause error at front end but I want to handle this error.
Looking forward for your response.
This discussion has been closed.
Replies
I got the answer I should have read about this fnServerData. Sorry about that.
[code]
$(document).ready(function() {
$('#example').dataTable( {
"sAjaxSource": "TestServlet",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"success": function(result){alert(result.sEcho);},
"failure":function(result){alert(result.sEcho);}
} );
}
} );
} );
[/code]
Now,I can control the behavior at front end by sending different sEcho value.