Arizona1911
Arizona1911

Reputation: 2201

Session timeout and AJAX in ASP.NET

I have a button that executes a script using AJAX. Normally when a session is still active the script will return some data that will be placed inside the parent page. If the session expired the AJAX will return the login screen which gets placed inside the parent page which looks really odd.

How would I be able to detect a session timeout and do a postback on the parent page?

Upvotes: 3

Views: 1057

Answers (3)

Kev
Kev

Reputation: 21

Since you are unlikely to be calling a full page, and you login page is likely to be a full page, you could just do the following.

if (xmlhttp.responseText.indexOf("DOCTYPE") != -1) {
    window.location.href = window.location.href;
}

Upvotes: 2

James Gaunt
James Gaunt

Reputation: 14783

What do you mean by 'if the session expired the AJAX will return the login screen'? You are in control of what is returned, so instead of returning the login screen return some sort of error code, or better throw an exception which you can catch as an error on the client.

Upvotes: 0

MCain
MCain

Reputation: 465

When you make your call, first check to see if one of your session parameters is Nothing/null. If it is null, then your session has likely timed out. If you don't have any session variables that you explicitly set, you can set one when the user logs in.

Upvotes: 0

Related Questions