[英]How do I get the response from a 400 error using Restangular
I'm having a problem getting the response from a 400 error header with restangular. 我遇到一个问题,从带有Restangular的400错误标头获取响应。 The code I've written is: 我写的代码是:
var loginData = Restangular.all("login");
$scope.postForm = function() {
loginData.post($scope.formData).then(function(response){
var error = response.error;
if(error) {
$scope.error = response.message;
} else {
localStorageService.clearAll();
localStorageService.set('xxx', response.id);
localStorageService.set('xxx', response.apiKey);
$location.path('/main');
}
});
}
When I get back a 200 header the $scope.error gets the response.message but not when the response header is 400. I set up the setErrorInterceptor() to log the message to console but I can't figure out how to get it to return the response so I can set the $scope.header. 当我返回200标头时,$ scope.error会获取response.message,但当响应标头为400时却不会。我设置了setErrorInterceptor()将消息记录到控制台,但我不知道如何获取它返回响应,所以我可以设置$ scope.header。
Edit: this is how I log the message to console in setErrorInterceptor() 编辑:这是我将消息记录到setErrorInterceptor()中的控制台的方式
RestangularProvider.setErrorInterceptor(
function(response) {
if (response.status == 401) {
console.log("Login required... ");
$window.location.href='/login';
} else if (response.status == 400) {
console.log(response.data.message);
} else {
console.log("Response received with HTTP error code: " + response.status );
}
return false; // I also tried commenting return false out
});
Check How can I handle errors in the FAQ. 在常见问题解答中检查如何处理错误 。 You need to pass in a second function into the then()
method of the promise to act as an error handler. 您需要将第二个函数传递给promise的then()
方法以充当错误处理程序。
Restangular.all("accounts").getList().then(function() {
console.log("All ok");
}, function(response) {
console.log("Error with status code", response.status);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.