我不知道角度发生了什么,它没有捕捉错误响应,我在这里看到有关此问题而没有答案。
我有一个简单的http请求,我得到400响应,我想在用户发生时向用户显示一条消息,问题是200的部分被执行。
我尝试过两种方式:
this.forgatPassword = function(data) {
return $http.post('http://someaddress/ForgotPassword', data);
}
$scope.forgatPassword = function() {
AuthService.forgatPassword($scope.user).then(function(res) {
$scope.message = true;
}, function(error) {
console.log('rejected');
});
}
$scope.forgatPassword = function() {
AuthService.forgatPassword($scope.user).then(function(res) {
$scope.message = true;
}).catch( function(error) {
console.log('rejected');
});
}
答案 0 :(得分:5)
我有同样的问题,我发现在我的auth拦截器中我检查响应状态。如果响应状态是401我做了什么,但我忘了设置是否不返回承诺,所以拦截器不会转发拒绝。
因此,请确保您最终拥有此代码:
return $q.reject(rejection);
答案 1 :(得分:0)
试试这样。
AuthService.forgatPassword($scope.user)
.success(function(res) {
$scope.message = true;
})
.error( function(error) {
console.log('rejected');
});
答案 2 :(得分:0)
您可以在服务中注入$ q并手动返回承诺。
this.forgatPassword = function(data) {
var q = $q.defer();
$http.post('http://someaddress/ForgotPassword', data)
.then(function(response) {
q.resolve(response.data);
},
function(error) {
q.reject(error);
});
return q.promise;
};
$scope.forgatPassword = function() {
AuthService.forgatPassword($scope.user).then(function(res) {
$scope.message = true;
}, function(error) {
console.log('rejected');
});
}