简体   繁体   English

Angular $ http错误回调响应始终未定义

[英]Angular $http error callback response is always undefined

I am having issues trying to gracefully handle $http errors. 我在尝试优雅地处理$ http错误时遇到问题。 I am looping over a list of servers to make API calls to for status. 我循环遍历服务器列表以进行状态的API调用。 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: 这是代码片段:

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. 我想要做的是没有Angular在日志中显示401,而是我将以优雅的方式显示它。 However since data is undefined I have no way of accessing the information. 但是,由于数据未定义,我无法访问信息。

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. 我可能做了一些明显错误的事情,但我不确定还有什么可以尝试的。

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. 如果要记录错误代码,请改为记录status参数。 It contains the HTTP Status Code, which should be uniform, unlike response bodies. 它包含HTTP状态代码,与响应主体不同,它应该是统一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM