[英]$http request. Response comes to success function even when status code is 401
In my Angular app I have a login function. 在我的Angular应用中,我具有登录功能。 But when I send wrong credentials and response come with status 401: Bad Credentials (there's even response.status = 401) it still goes to success. 但是,当我发送错误的凭据并且响应带有状态401:错误的凭据(甚至有response.status = 401)时,它仍然可以成功。
So I'm getting $notify "Success login and then html error page in my interceptor. That's confusing. I'm not sure what I've done to create this mess. 因此,我得到了$ notify“成功登录,然后在拦截器中显示html错误页面。这很令人困惑。我不确定要如何创建此混乱状态。
this.getTokenCustom = function (user) {
$http.post('/login',
JSON.stringify({username: user.username, password: user.password}))
.then(
function success(response) {
localStorage.setItem('token', response.data.token);
$.notify({message: "Success login"},{type:'success'});
$state.go('roles');
},
function error(data) {
console.log(data);
$.notify({message: data.data.message},{type:'danger'});
}
);
};
UPD UPD
this.getTokenCustom = function (user) {
$http.post('/login',
JSON.stringify({username: user.username, password: user.password}))
.then(
function success(response) {
localStorage.setItem('token', response.data.token);
$.notify({message: response.status + " Success login"},{type:'success'});
$state.go('roles');
},
function error(data) {
console.log(data);
$.notify({message: data.data.message},{type:'danger'});
}
);
};
The likely cause of this is that you may have a custom interceptor that doesn't handle error conditions properly. 造成这种情况的可能原因是,您可能有一个自定义拦截器,无法正确处理错误情况。
This post can point you on the right direction: http://www.jamessturtevant.com/posts/AngularJS-HTTP-Service-Success-Handler-400-Response-Code-and-Interceptors/ 这篇文章可以为您指明正确的方向: http : //www.jamessturtevant.com/posts/AngularJS-HTTP-Service-Success-Handler-400-Response-Code-and-Interceptors/
To quote the post: 引用帖子:
When you handle the requestError or the responseError for a interceptor and you wish to pass the error on to the next handler you must use the promise api to reject the message: 当您处理拦截器的requestError或responseError并希望将错误传递给下一个处理程序时,必须使用promise api拒绝消息:
$httpProvider.interceptors.push(function($q) {
return {
'requestError': function(rejection) {
// handle same as below
},
'responseError': function(rejection) {
if (canRecover(rejection)) {
// if you can recover then don't call q.reject()
// will go to success handlers
return responseOrNewPromise;
}
// !!Important Must use promise api's q.reject()
// to properly implement this interceptor
// or the response will go the success handler of the caller
return $q.reject(rejection);
}
};
});
A similar issue to yours was reported on Angular's Github repository and the issue had to do with a custom interceptor not handling error conditions properly. 在Angular的Github 存储库中报告了与您类似的问题 ,该问题与自定义拦截器有关,无法正确处理错误情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.