javascript Angular $http 错误回调响应始终未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18130878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular $http error callback response is always undefined
提问by Chuck M
I am having issues trying to gracefully handle $http errors. I am looping over a list of servers to make API calls to for status. The calls that complete successfully for perfectly. The ones that fail are not giving me access to the error information. It is always undefined. Here is the code snippet:
我在尝试优雅地处理 $http 错误时遇到问题。我正在遍历服务器列表以进行 API 调用以获取状态。完美完成的调用。失败的那些并没有让我访问错误信息。它总是未定义的。这是代码片段:
angular.forEach($scope.servers, function (server) {
// blank out results first
server.statusResults = {};
$http.jsonp(server.url + '/api/system/status?callback=JSON_CALLBACK', {headers: { 'APP-API-Key': server.apiKey }}).
success(function (data, status, headers, config) {
server.statusResults = data;
}).
error(function (data, status, headers, config) {
// data is always undefined here when there is an error
console.error('Error fetching feed:', data);
});
}
);
The console output shows the correct 401 error (which I didn't output) and my console error message (which I did output) with an undefined data object.
控制台输出显示了正确的 401 错误(我没有输出)和我的控制台错误消息(我输出了)以及一个未定义的数据对象。
GET https://server_address/api/system/status?callback=angular.callbacks._1 401 (Unauthorized) angular.min.js:104
Error fetching feed: undefined
What I am trying to do is NOT have Angular display the 401 in the log, and instead I will display it in a graceful way. However since data is undefined I have no way of accessing the information.
我想要做的不是让 Angular 在日志中显示 401,而是以优雅的方式显示它。但是,由于数据未定义,我无法访问这些信息。
I am new to AngularJS, but my example closely matches other examples I've found in the documentation.
我是 AngularJS 的新手,但我的示例与我在文档中找到的其他示例非常匹配。
I've also tried using $resource instead of the $http and got the exact same problem.
我也尝试过使用 $resource 而不是 $http 并遇到了完全相同的问题。
var statusResource = $resource(server.url + '/api/system/status', {alt: 'json', callback: 'JSON_CALLBACK'},
{ status: {method: 'JSONP'}, isArray: false, headers: { 'APP-API-Key': server.apiKey } });
// make status API call
statusResource.status({}, function (data) {
server.statusResults = data;
}, function (err) {
// data is always undefined here when there is an error
console.log(err);
});
I'm probably doing something obviously wrong, but I'm not sure what else to try.
我可能正在做一些明显错误的事情,但我不确定还能尝试什么。
采纳答案by iagreen
Per the $http
docs, body
is
根据$http
文档,body
是
The response body transformed with the transform functions.
使用转换函数转换的响应主体。
With the 401 (Unauthorized) error you are getting back, it is quite possible there is no body being returned, hence why it is undefined.
返回 401(未授权)错误后,很可能没有返回正文,因此它是未定义的。
If you want to log the error code, log the status
parameter instead. It contains the HTTP Status Code, which should be uniform, unlike response bodies.
如果要记录错误代码,请status
改为记录参数。它包含 HTTP 状态代码,与响应主体不同,它应该是统一的。