When a logged user click the signout link I'd like to call the logout action 开发者_开发技巧without refresh the page or redirect. Any helps? Thanks!
You need to simply bind some code to your logout link/button that requests your logout action:
$("#logout").click(function() {
$.get("/foocontroller/logout", function() {
alert('Successfully logged out');
});
return false; // so the page does not refresh
});
Returning false from the click event prevents the default 'link following' behaviour which would typically cause a refresh/redirect.
You can even do it in a more unobtrusive way:
$('a.logout').click(function () {
var logoutUrl = $(this).attr('href');
$.get(logoutUrl, function () {
alert('Logged out');
});
return false;
});
What this does is it simply finds the logout link ($('a.logout')
) and when you click the link will get "followed" in the back, without changing the page (the $(this).attr('href')
part will get the url from the link). Replace alert('Logged out');
with what you want to happen after the user has been logged out.
精彩评论