jquery ajax常用的登录登出
整理jquery+ajax的登录登出方法。
//登录
var currentUserId = -1;
$(function() {
var timestamp = (new Date()).valueOf();
$.ajax({
type: 'POST',
url: '../m/user/getCurrentUser',
dataType: 'json',
data: {
t: timestamp
},
success: function(data) {
var isOK = false;
if(data.code == 1) {
var currentUser = data.object;
if(currentUser) {
$(".to_login").html('<a href="#">退出登录</a>');
$(".to_login").click(function() {
logout();
});
$(".wel_username").html("欢迎您,<span >" + currentUser.userName + "</span>");
currentUserId = currentUser.userId;
$(".get_username").html(currentUser.userName);
if(currentUser.userId != 1) {
$("#systemManage").hide();
}
if(currentUser.roleId == 3) {
isOK = true;
} else {
logout();
}
}
}
//登录失败
if(!isOK) {
window.location.href = "login.html";
}
}
});
});
//登出
function logout() {
$.ajax({
type: 'post',
url: '../m/user/logout',
dataType: 'json',
success: function(data) {
if(data.code == 1) {
window.location.href = "login.html";
}
}
})
}
//首页
$(document).ready(function() {
$('.to_login').click(function() {
$.cookie("autoLogin", 0);
$.cookie("password", "");
});
})