有时,我们的后台接口有权限的限制,不满足权限,就返回错误码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;  
       }  

 

相关文章:

  • 2022-12-23
  • 2021-05-30
  • 2021-12-14
  • 2021-07-01
  • 2022-12-23
  • 2021-05-06
  • 2021-11-30
  • 2022-02-22
猜你喜欢
  • 2021-05-27
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2021-11-08
相关资源
相似解决方案