views:

191

answers:

2

I have this function that utilizes jQuery's post feature to send an ajax request to my logout handler, which destroys the session (set by asp.net) and redirects to the login page:

<script type="text/javascript">
    //<![CDATA[
    function doLogout() {
        var conf = false;
        conf = confirm("Really log out?");
        if (conf === true) {
            $.post("logout.ashx");
        }
    }
    //]]>
</script>

So far, the logout link functions fine: user clicks it, the confirmation box pops up, and if they click yes it loads up the logout handler:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    With context
        If .Session IsNot Nothing Then
            context.Session.Clear()
            context.Session.Abandon()
        End If
        .Server.Transfer("default.aspx", False)
    End With
End Sub

I have tried using Response.Redirect as well, nothing different happens.

I have verified by tracing that everything is functioning fine. There are no javascript or asp.net errors. The only problem is the page in the browser does not change.

Any ideas?

+1  A: 

When you post an ajax request you can redirect at the server all you want, but all you are doing is determining which page ultimately responds to that ajax request. If you want to redirect the entire page, you need to post from the entire page, or build the javascript that handles the response to know when you'll want to redirect and do it there.

Joel Coehoorn
A: 

The problem is that the callback to the server is an asynchronous javascript call and not a page postback. if you wish to change the page in the browser you will also have to do this in your javascript function after the call to your logout function.

Andy Rose
so call a window.location([url])?
Anders
Yes, something along those lines although I would think you would need to ensure your logout function succeeded first. I think that the suggestion of just posting back to a logout page and doing the redirection there after the logging out has succeeded is probably the way to go.
Andy Rose