欢迎阅读『Tag标签列表』

401权限没有放开

有时,我们的后台接口有权限的限制,不满足权限,就返回错误码401,那么在js中如何判断呢?

if ((data.status && data.status == '401') || (data.statusText && data.statusText == 'No Transport')) {  
            console.log(data);  
            var currentUrl = window.location.href;  
            window.location.href = currentUrl;  
            return;  
        }  

下面是真实的返回:

Object {readyState: 4, responseText: "", status: 401, statusText: "Unauthorized"}

 {readyState: 4, responseText: "", status: 401, statusText: "Unauthorized"}

java后台代码:

spring MVC拦截器中的部分代码:

if(StringUtil.isNullOrEmpty(token)){//added by huangwei  
                        logger.error("token is null");  
                        response.setStatus(401);  
                        return false;  
                    }

 优化如下:

Js代码
if ((data.status && data.status == '401') || (data.statusText && (data.statusText == 'No Transport'||data.statusText == 'Unauthorized'))) {  
           console.log(data);  
           var currentUrl = window.location.href;  
           if(currentUrl){  
               currentUrl=currentUrl.replace(/#$/,'');  
           }  
           window.location.href = currentUrl;  
           return;  
       }  

 

posted @ 2019-03-08 17:32  兔子零-A酱  阅读(1267)  评论(0编辑  收藏  举报