How to do LogOff with Ajax?

0

I have my menu, and it has a dropdown

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Usuário</a>
    <ul class="dropdown-menu" role="menu">
        <li><a href="/">Meus dados</a></li>
        <li><a href="#" id="LogOff">Sair</a></li>
    </ul>
</li>

I tried to use the "Exit" in this way

using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm"}))
{
@Html.AntiForgeryToken()

<ul class="nav navbar-nav">

    <li><a href="javascript:document.getElementById('logoutForm').submit()">Sair</a></li>
</ul>
}

But it breaks with the menu by rendering a Form

Then I tried with Ajax

$("#LogOff").on("click", function (e) {
    $.post('@Url.Content("~/Account/LogOff")');
});

So he sends the Post, but I have to give "Refresh" to login, ignoring the RedirectToAction that is in action LogOff and also does not accept ValidateAntiForgeryToken

    
asked by anonymous 04.09.2014 / 18:28

1 answer

2

Redirect will not work for ajax requests. You have to separate it into two parts.

Ex:

<script>
    $(function(){
        $("#logoutForm").click(function(e){
            e.preventDefault();

            $.post('@Url.Content("~/Account/LogOff")', null, function(){
                window.location = '/Account/Login';
            });

        });

    });
</script>
    
04.09.2014 / 20:25