有时,我们的后台接口有权限的限制,不满足权限,就返回错误码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"}
Java后台代码:
spring MVC拦截器中的部分代码:
- if(StringUtil.isNullOrEmpty(token)){//added by huangwei
- logger.error("token is null");
- response.setStatus(401);
- return false;
- }