为什么$ httpProvider.interceptors返回一个`undefined`值

时间:2015-10-11 22:45:17

标签: javascript angularjs api angular-http angular-http-interceptors

我创建了一个基本的AngularJS应用程序,该应用程序使用了Yelp的API,并且无法使用$httpProvider.interceptors来解析我的回复。

这是我的应用:

var app = angular.module("restaurantList", []);

我的yelpAPI服务(未图示)验证我的API请求并生成HTTP请求。然后我将收到的数据输出到Web控制台,如下所示:

app.controller("mainCtrl", ["$scope", "yelpAPI", function ($scope, yelpAPI) {
    $scope.restaurants = [];
    yelpAPI.get(function (data) {
        $scope.restaurant = data;

        console.log($scope.restaurant);

    });

}]);

以下是我的请求中的数据:

Object {region: Object, total: 37, businesses: Array[20]}

我希望数组位于businesses属性中。所以,我认为使用$httpProvider.interceptors来解析Object.businesses数组中找到的对象是个好主意。

以下是$httpProvider.interceptors在我提出初始请求时的样子:

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push(function () {
        return {
            response: function (response) {

                return response;
            }
        }
    });
});

以下是$httpProvider.interceptors现在的样子:

app.config(function($httpProvider) {
    $httpProvider.interceptors.push(function() {
        return {
            response: function(response) {

                var old_response = response.businesses,
                    new_response = [];


                for (var i = 0; i < old_response.length; i++) {

                    var obj = old_response[i],

                        new_obj = {
                            restaurant_name: obj.name,
                            phone_number: obj.display_phone,
                            yelp_rating: obj.rating,
                            reservation_url: obj.reservation_url
                        };

                    new_response.push(new_obj);
                }

                return new_response;
            }
        }
    });
});

现在,我收到的错误是TypeError: Cannot read property 'businesses' of undefined。有什么我可以忽略的吗?

编辑#1

我在拦截器中使用了console.log(response)来打印我的回复,发现response.businesses实际上应该是response.data.businesses。这解决了我的错误,但现在我的$http调用返回undefined。知道我的新问题是什么吗?

编辑#2

app.factory("yelpAPI", function($http, nounce) {
    return {
        get: function(callback) {
            var method = "GET",
                url = "http://api.yelp.com/v2/search";
            var params = {
                callback: "angular.callbacks._0",
                oauth_consumer_key: "my_oauth_consumer_key",
                oauth_token: "my_oauth_token",
                oauth_signature_method: "HMAC-SHA1",
                oauth_timestamp: new Date().getTime(),
                oauth_nonce: nounce.generate(),
                term: "American",
                sort: 2,
                limit: 20,
                radius_filter: 4000,
                deals_filter: true,
                actionlinks: true
            };
            var consumerSecret = "my_consumer_secret",
                tokenSecret = "my_token_secret",
                signature = oauthSignature.generate(method, url, params, consumerSecret, tokenSecret, {
                    encodeSignature: false
                });
            params["oauth_signature"] = signature;
            $http.jsonp(url, {
                params: params
            }).success(callback);
        }
    }
});

2 个答案:

答案 0 :(得分:1)

使用{data:}:

返回角度等待对象
app.config(function($httpProvider) {
    $httpProvider.interceptors.push(function() {
        return {
            response: function(res) {

                var old_response = res.businesses,
                    new_response = [];


                for (var i = 0; i < old_response.length; i++) {

                    var obj = old_response[i],

                        new_obj = {
                            restaurant_name: obj.name,
                            phone_number: obj.display_phone,
                            yelp_rating: obj.rating,
                            reservation_url: obj.reservation_url
                        };

                    new_response.push(new_obj);
                }

                return {data : new_response};
            }
        }
    });
});

返回{data:new_response}

答案 1 :(得分:0)

Emir帮助我解决了这个问题,但实际上,每当你使用$httpProvider.interceptors时,你必须更新response.data并返回整个响应对象(即不仅仅是一个新的数组,因为我做了)因为{{ 1}}为您选择$http属性。