首页 > 代码库 > 重写ajax方法实现异步请求session过期时跳转登录页面
重写ajax方法实现异步请求session过期时跳转登录页面
jQuery(function($){ // 备份jquery的ajax方法 var _ajax=$.ajax; // 重写ajax方法, $.ajax=function(opt){ var _success = opt && opt.success || function(a, b){}; var _error = opt && opt.error || function(a, b){}; var _opt = $.extend(opt, { success:function(data, textStatus){ // 如果后台将请求重定向到了登录页,则data里面存放的就是登录页的源码,这里需要判断(登录页面一般是源码,所以这里只判断是否有html标签) if(data.meta.code == 返回的需要登录的状态码) { alert(‘请先登录!‘); window.location.href = "http://www.mamicode.com/login.html"; return; } _success(data, textStatus); }, error:function(data, textStatus){ if(data.meta.code == 返回的需要登录的状态码){ alert(‘请先登录!‘); window.location.href = "http://www.mamicode.com/login.html"; return; } _error(data, textStatus); } }); return _ajax(_opt); }; });
重写ajax方法实现异步请求session过期时跳转登录页面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。