[Django]-How can an Ajax callback realize that a user's authenticated session has timed out?

7👍

One way would be to have a script that just pings a page to see whether or not it’s still logged in, but this would increase the number of AJAX requests you’d have to do.

Another option is to have the complete function of $.ajax() check the HTTP status code on the XMLHttpRequest object that is passed in:

$.ajax({..., complete = function(xhr, textStatus){
    if(xhr.status > 300 && xhr.status < 400) //you got a redirect
        window.location.href = '/login/';
    ...
    }, ...);
👤tghw

1👍

Simple really, have whatever script you’re querying return some sort of error code, maybe

{'error', 'notloggedin'}

then, all you need to do is check for this value in your javascript. If it’s found, you then do something like

window.location.href = '/login/';

to make the javascript redirect to the login page (changing the path as necessary)

👤Mez

Leave a comment