js 判断ajax请求的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;
                    }

 优化如下:

 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;
        }

猜你喜欢

转载自hw1287789687.iteye.com/blog/2235377