On Ajax Error Do Something

On Ajax Error Do Something

jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
edited September 2011 in General
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?

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    this isn't exactly what you were hoping for, but you can examine the json contents in fnServerData before calling fnCallback. if the results look like the login page, make a redirect.
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    could you by chance point me to an example of this?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) {
    if (json.match(/login/i)) location.href = "login.htm";
    else fnCallback(json);
    } );
    },
    [/code]
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    thanks i will give this a shot now, much appreciated.
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    edited September 2011
    I have the following code but get the following error

    [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]
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    edited September 2011
    although I do have to state one thing. When the /resourceajax/ controller is called it generally will output JSON for the datatable however when you are not logged in it will redirect you to the login page.

    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?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Won't know until you try. Not sure if the $.getJSON tries to force it into json. Your debugger will give you clues as to what to look for.
  • GregPGregP Posts: 500Questions: 10Answers: 0
    We have a similar scenario. We haven't found the optimal solution yet (handling sessions can be tricky!) but here's what we're doing so far (using Java servlets on the server side for data, and JSPs for the pages themselves):

    - 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.
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    GregP: Thanks for that tip. I went the route of checking for an empty object and redirect on that. I tried to click the external buttons/links but was unable to be quick enough to trigger the error you saw in your implementation :)
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Heheh, hey, we're programmers. Just have to asynchronously and programmatically trigger the click() with javascript. ;-)

    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. :)
  • GregPGregP Posts: 500Questions: 10Answers: 0
    A bit more follow-up having read the original question again. If you already have session management in place, I wouldn't bother hooking into DataTables for your scenario.

    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]
  • jcrawfordjcrawford Posts: 172Questions: 0Answers: 0
    thanks for that tip, i might try to work that in when i get more time to work on that :)
This discussion has been closed.