一个很常见的问题,如果用户登录网站session过期,需要用户返回登录页面重新登录。

如果是http请求可以在后台设置拦截器,统一拦截并跳转。但是ajax方法并不能通过后台直接跳转。

所以我们可以写一个ajax全局方法,让每一个ajax请求都访问这个方法,如果经过判断session过期,可以跳转到登录页面。

以下是引用jQuery的ajax全局方法:

$.ajaxSetup({
    error : function(x, status, e) {
        if (x.status == '403') {
            alert('您没有权限执行当前操作!');
        } else {
            console.log('系统错误,请联系管理员!');
        }
        return false;
    },
    complete : function(r, s) {
        var istimeout = r.getResponseHeader('sessionstatus')=='timeout';
        if (istimeout) {
            //alert(r.responseText);
            var loginurl = r.getResponseHeader("loginurl");
            parent.window.location = loginurl;
        }
    }
});

 

我当时做这个项目的时候还用到了vue,所以把vue的ajax全局方法也写一下

    Vue.http.interceptors.push(function(request) {

            // modify request
            request.method = 'POST';

            // return response callback
            return function(response) {
                var sessionAjax = response.headers.map.sessionstatus;
                if (sessionAjax != undefined) {
                    var loginurl = response.headers.map.loginurl[0];
                    parent.window.location = loginurl;
                }
            };
        });

 

相关文章:

  • 2021-09-27
  • 2021-11-28
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2021-11-28
猜你喜欢
  • 2021-08-31
  • 2022-02-12
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
相关资源
相似解决方案