【发布时间】:2018-05-31 00:14:48
【问题描述】:
我在 angularJS 中创建了一个处理 get 和 post 请求的服务,但问题出在我的控制器中,我无法捕捉到错误回调但是成功回调运行良好:
app.js
AuthenticationService.Login(function (_data) {
$rootScope.showPreloader = false;
console.log(_data)//works fine when call is succes
},function (error){
console.log(error)//not executed when error is thrown
});
authentication.service.js
function Login(callback) {
$http({
method: "GET",
url: "/auth/getRoles",
headers: {
"Accept-Language": "en-US, en;q=0.8",
"Content-Type": "application/json;charset=UTF-8"
}
}).then(function (_response) {
return _response.data;
var serverData = response.data;
callback(serverData);//getting respone is controller when succes
}, _handleError);
};
function _handleError(_error) {
console.log(_error)//console prints on error
return _error//controller never recevies the return statement
};
问题出在 handleError 函数中,该函数永远不会将错误返回给控制器,任何帮助都将得到极大的赞赏
【问题讨论】:
标签: angularjs angularjs-service angularjs-http