[RESOLVED]Session Timeout Issue

Hi, All

I have a web application using MVC 4 and N-Hibernate , Most of my pages are submited by  jquery ajax call. but when my session is time out my ajax call not performing. It generate error in browser console.I have a custom error page which I call after session
timeout. But when any client side call happen it not go to the custom error page.So I cannot handle it.

Please anyone suggest me how to handle session timeout when ajax call happen or any client side call occure.

You haven’t explained what "ajax call not preforming" means so it is hard to give an exact answer, but you need to handle whatever happens when you make the call.  If an error is being thrown then use the "error" setting of the jQuery ajax call to do something
like notify the user or direct off to an error page.  If the call is working but the data is coming back empty then detect that there is no data and again notify the user or direct off to a different page.

Actually I mean that I have post the form to the controller through ajax call,but if the session is timeout the ajax request not performing,i.e the request not hit the controller action method.So I want to redirect my custom error page when session is time
out. It should work for ajax call and other client side event.

I don’t understand how the session timing out will stop your action being called, you’d need to post your code.  Unless you’re talking about authentication and not the session.

Add the below code in you main page possibly layout or if you have a main home.cshtml

<script>
    $(document).ajaxError(function (event, xhr, props, error) {
        if (xhr.status === 401) {
            alert("Session timed out");
            location.reload();
        }
    });
</script>

 

Hi ajaya,

For the AJAX request, it won’t redirect current page to other page, you need to have logic to handle it. The reason of the request not hit the controller action method is that that request has been redirect to custom error page.

I suggest that you need have the logic for the AJAX request when the session time out. Just capture the
ajaxError may not work if the request has already been redirect to other page.

Best Regards

Starain

Leave a Reply