整理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", "");

});

})

最新文章

  1. 【面试题】TB
  2. System.Threading.ThreadAbortException: 正在中止线程。
  3. java转义字符(转载)
  4. Java学习----日期函数
  5. css考核点整理(四)-css盒模型
  6. (导航控制器view)全屏幕滑动实现pop效果
  7. Js内存泄露问题总结
  8. dotnet的移除--&gt;安装 升级
  9. Python基础学习7---异常处理
  10. Javaweb阶段知识回顾一
  11. python自动安装mysql5.7
  12. Xen的虚拟化详解
  13. C++之标准库map
  14. Avalon MM 总线
  15. 正则表达式在python中的简单使用
  16. 平衡二叉树的java实现
  17. 好汉两个半第一季/全集Two And A Half Men迅雷下载
  18. 微信小程序之顶部固定和底部固定
  19. lua 工具类(一)
  20. EXPAT(XML解析库)

热门文章

  1. 51nod 1099:任务执行顺序 贪心
  2. Jmeter测试入门——带token的http请求
  3. C++面试常见问题——11重载、覆盖、隐藏
  4. Swift Access Levels (变量关键字访问权限)
  5. GoJS最简单的实例
  6. java学习之IO流(学习之旅,一)
  7. DStream-05 updateStateByKey函数的原理和源码
  8. 吴裕雄--天生自然java开发常用类库学习笔记:System类
  9. DevOps - 优势
  10. CSS隐藏商务通等内容