javascript – $http.get成功调用错误函数
作者:互联网
我没有能力更改端点,如果失败,它将返回200 OK响应,其中包含不同的数据.如何让它运行错误函数(来自promise的第二个函数)?
我的服务:
self.getData = function() {
return $http.get('/api/controller/action').then(function(x) {
//This will be run even on failure. How can I call....
return x.data;
}, function(x) {
//......here, from the front-end, so that, the 2nd function in
//inside the 'then' in the controller is run.
//This code will currently never run as it never fails server side.
return x;
});
};
控制器:
MyService.getData().then(function(x) {
//Success
}, function(x) {
//Failure
});
解决方法:
使用$q.reject,例如
return $http.get('/api/controller/action').then(function(x) {
if (x.data === 'some error condition') {
return $q.reject(x);
}
标签:javascript,angularjs,angularjs-directive,angular-promise 来源: https://codeday.me/bug/20190611/1219084.html