Atul Yadav
Atul Yadav

Reputation: 496

Check Session before every Jquery Ajax hit

I have implemented a project in MVC and there i have done most of the thing with ajax request. but we are facing a issue of session time out. So i want some thing with ajax that before doing any ajax request it check the session and then process that ajax request.

I do't want to write if else condition in all the page where i uses the ajax.

Please suggest some way.

Thanks Atul

Upvotes: 2

Views: 8065

Answers (5)

Arun P Johny
Arun P Johny

Reputation: 388396

I had the same problem with jQuery-ajax, spring mvc and spring security architecture.

The solution I followed was as follows.

If a session timeout occurs spring security will redirect the request to the login form controller.

In the login controller check whether the request(Saved Request) is an ajax request(presents of header x-requested-with), if it is an ajax request return an json/xml value indicating the timeout. ex: {"session-timeout" : true}.

Then test this ajax returned value using an global jQuery ajax event like ajaxSuccess/ajaxError to see whether there was an session timeout and take what ever actions you want to take.

You can try something like this using what ever application framework you are using.

Upvotes: 0

Pedro Moreira
Pedro Moreira

Reputation: 970

I've been around this issue today and this is my solution:

This php part (file or controller method) will be called by ajax:

// create your preferred way of returning a json object like { "isLogged": true }
echo json_encode(array('isLogged' => checkSession()));

Now, put this script in the beginning of all of your pages that have ajax calls and require the user to be logged:

$(document).ajaxSend(function(e, xhr) {
    $.ajax({
        url: 'checkSession.php',
        type: 'GET',
        global: false,
        success: function(session) {
            if(!session.isLogged) {
                xhr.abort();

                // This allows my framework to take care of the redirect
                // and to remember the intended page,
                // so it redirects back here after login:
                location.reload();

                // use this one to redirect directly to the login page:
                // window.location.replace('login.php');
            }
        },
        dataType: 'json'
    });
});

The global: false part is essential here, to prevent this ajax call to enter the ajaxSend and therefore create an infinite loop.

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

You could create a global filter; a global filter affects every controller action, can check whether session exists, and then redirect to the login page or do something in accordance. Also, you can keep the session alive instead by creating a view that you ping with AJAX or an iframe that keeps the requests to the server going, so the session stays alive. The question is do you want session to stay alive that long then...

Upvotes: 0

Bay
Bay

Reputation: 205

Maybe you should check jQuery.ajaxPrefilter() ( http://api.jquery.com/jQuery.ajaxPrefilter/ ). Also can't you modify your Application Pool settings regarding Idle Time-out to avoid the session timeout altogether?

Upvotes: 1

Vapire
Vapire

Reputation: 4578

One way I've done it in a project was to implement a keep-alive ajax request which gets sent in the background at a certain interval... so your session doesn't get timed out...

Maybe not a very elegant way but it worked :)

Upvotes: 0

Related Questions