我想從控制器中整齊地管理服務函數調用,同時處理來自API的錯誤。
然而,當我這樣做,我仍然得到this is showing even with 403 and 404 errors
在我的控制檯,即使API拋出回403或404
我猜想這可能是工作,如果我在我的服務文件添加catch
但我會寧願保持這種管理從控制器。這可能嗎?
控制器:
angular.module('EnterDataCtrl', []).controller('EnterDataController', ['$scope', 'Data', '$state', function ($scope, Data, $state) {
vm = this;
vm.getRules = function (e, rule_query, data) {
if (e.keyCode == 13) {
Data.getRules(rule_query,data).then(function (data) {
console.log('this is showing even with 403 and 404 errors');
}).catch(function(res) {
console.log(res);
});
}
}
}]);
服務:
angular.module('EnterDataService', []).factory('Data', ['$http', function ($http) {
return {
getRules: function getRules(rule_query,data) {
var apiBase = apiUrl + 'get-rules';
var config = {
handleError:true,
params: {
rule_query: rule_query,
data : data
}
};
return $http.get(apiBase, config).catch(function() {});
}
}
}]);
的可能的複製[錯誤狀態代碼不被抓住](http://stackoverflow.com/questions/39166536/error-status-codes-are-not-being-caught)。當拒絕處理程序省略'throw'語句時,它返回'undefined'。這**將被拒絕的承諾轉化爲履行承諾,並解決爲未定義的承諾。 – georgeawg