Reputation: 496
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
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
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
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
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
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