On Ajax Error Do Something
On Ajax Error Do Something
Hello Everyone,
I have a datatable used in an admin area. The page is all run by ajax including the table which uses server side processing. Here's my dilemma. When a user is logged in and remains on that page for a long time without activity the user will be automatically logged out of the system by the session handling. When the user returns to their computer they are still on the page which shows the datatable.
When they click to go to the next page I get a JSON formatting error. This is because the system does not think they are logged in. So you have an ajax request which is made but is actually returning the HTML for the login page.
Is there a way to "hook" into the datatables so that when this happens I can make the entire page redirect to the login page rather than showing a JSON error alert box?
I have a datatable used in an admin area. The page is all run by ajax including the table which uses server side processing. Here's my dilemma. When a user is logged in and remains on that page for a long time without activity the user will be automatically logged out of the system by the session handling. When the user returns to their computer they are still on the page which shows the datatable.
When they click to go to the next page I get a JSON formatting error. This is because the system does not think they are logged in. So you have an ajax request which is made but is actually returning the HTML for the login page.
Is there a way to "hook" into the datatables so that when this happens I can make the entire page redirect to the login page rather than showing a JSON error alert box?
This discussion has been closed.
Replies
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.getJSON( sSource, aoData, function (json) {
if (json.match(/login/i)) location.href = "login.htm";
else fnCallback(json);
} );
},
[/code]
[code]
"fnServerData": function( sSource, aoData, fnCallback) {
$.getJSON(sSource, aoData, function(json) {
if(json.match(/login/i))
location.href = '/admin/login/';
else
fnCallback(json);
});
},
[/code]
json.match is not a function
if I do alert(json) it returns [object Object]
Currently while getting the above error I am logged in so there should be no problem. However if I were not logged in the /resourceajax/ controller would do a redirect to the login page hence returning HTML and not JSON, would this still be effective?
- Some pages poll for new data every X seconds. If the servlet knows that a session is invalid for whatever reason, it returns a JSON object with no data in it. The JSON is "valid", so we can then process it; when we detect on the client side that there is no JSON, we assume that the session is invalid and redirect to the login page from the client side.
- If the session is invalid and the user requests a whole new page (clicking a navigation link), we do a server-side redirect, sending the user to the login page and display an 'session expired' message to the interface for the user to see.
Where this falls apart just a little is when the server goes down. Initially we were just using .ajax() and its error callback (makes sense; servlet isn't there, the request goes into error state, application redirects to login) but there came a problem:
If you click another link while in the middle of your DataTables AJAX request, the error callback is triggered and the user is redirected.
So, we had to drop that.
Still looking for the ultimate solution, but hopefully that will help on some level.
Just kidding. We came upon it merely because we poll. When you poll for changes every few seconds, you're likely to encounter it every now and then. :)
The user is on "page1.php" (or whatever) and leaves their desk. Two hours later, they return. Their session has expired, but they don't know it yet. They either click "refresh" or they go to "page2.php". It is THIS request that should be firing them back to the login page. If they get to the AJAX request by DT, they have gone too far.
That said, I just now discovered that if you use fnServerData to redefine your data request, you can use the statusCode parameter! Super-useful to handle server status (usually errors) pages:
[code]
statusCode: {
404: function() {
alert('page not found');
}
}
[/code]