[英]How can I retrieve HTTP status codes using Angular's $q promises?
I have within my code that makes an $http.post
to a back end, and of course things can go wrong. 我的代码中有一个将
$http.post
到后端的代码,当然可能会出错。 If I use the below syntax within angular: 如果我在angular中使用以下语法:
$http.post(url).success(function(data, status, headers, config))
.error(function(data, status, headers, config));
If there is an error, I can retrieve the error code using status
and handle it within the function defined in error()
. 如果有错误,我可以使用
status
检索错误代码,并在error()
定义的函数中处理它。 However, if instead I take this approach (which is the one I am in fact using): 但是,如果我改为采用这种方法(实际上是我正在使用的方法):
var deferred = $q.defer();
$http.post(url).success(deferred.resolve).error(deferred.reject);
return deferred.promise;
With the second syntax, I have all of my ajax calls within a separate ServiceJS
directory, and can handle the successes or errors on a case by case basis. 使用第二种语法,我所有的ajax调用都放在一个单独的
ServiceJS
目录中,并且可以根据情况处理成功或错误。 For instance, if the second snippet was Service.MyFunction()
then in the code where I need it I would: 例如,如果第二个片段是
Service.MyFunction()
那么在我需要的代码中,我将:
Service.MyFunction().then(function() {},
function(data, status, headers, config) {});
However, if I use this code block, status
, headers
, config
are all undefined. 但是,如果我使用此代码块,则
status
, headers
, config
都是未定义的。 My question is how can I retain this syntax but still retrieve the error code? 我的问题是如何保留此语法,但仍检索错误代码?
As an added reference, the back end is a C# Web API project, which would return errors using return BadRequest();
作为补充参考,后端是一个C#Web API项目,该项目将使用
return BadRequest();
返回错误return BadRequest();
or the like. 或类似的东西。
Try something like this: 尝试这样的事情:
myFunction(){
var deferred = $q.defer();
// you can use .then() instead of .success or .error
$http.post(url).then(function(successResponse){
var data = successResponse.data;
var status = successResponse.status;
...
deferred.resolve(successResponse);
}, function(failureResponse){
var status = failureResponse.status;
var config = failureResponse.config;
...
deferred.reject(failureResponse);
});
return deferred.promise;
}
well, I'd say it's a good practice and more standard to implement a http interceptor and handle the HTTP errors from there intead of handling the error one by one on each http or resource object, and your code will be located in a single location. 好吧,我想说这是一个很好的实践,是实现http拦截器并从那里处理HTTP错误的一种更好的标准,它涉及在每个http或资源对象上一一处理错误,并且您的代码将位于一个位置。
basically, you can segment the actions to take depending on the error status you get for example: 基本上,您可以根据所得到的错误状态来细分要采取的措施,例如:
angular.module('app').factory('myInterceptor', ['$q',
function($q){
return {
'responseError': function(rejection){
switch(rejection.status){
case 0:
//'No connection, is the internet down?'
break;
case 422:
// error
break;
case 484:
// error
break;
default:
// message rejection.status, rejection.statusText
break;
}
return $q.reject(rejection);
}
};
}
]);
$http
is already returning a promise, so why not use that one? $http
已经返回了一个承诺,那么为什么不使用那个承诺呢?
function myFunction() {
return $http.post(url);
}
// ...
myFunction().success(function(data, status, headers, config) {
// ...
});
... or ...
myFunction().then(...);
The promises returned from $http
have the methods success
and error
in addition to the other promise methods. 从
$http
返回的promise除了其他promise方法外,还具有success
和error
方法。 Read more at Angular docs for $http . 有关更多信息,请访问Angular docs中的$ http 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.