从http get请求获取空响应

时间:2016-05-18 08:58:19

标签: angularjs json node.js

我使用MEAN堆栈开发Web应用程序。 Nodejs用于创建服务器和连接Mongodb。我需要在这个网址的前端显示json数据" http://www.booking.com/autocomplete?lang=en-us&aid=304142&term=tha"然后在mongodb中添加它。所以我一直在使用angularjs的http服务,但是当浏览器显示它完全没问题时获得空响应。 我尝试过添加标头和每个用户代理。我尝试过不同的浏览器(Chrome,firefox)。即使是python也会得到非空的json。它不仅仅适用于angularjs。这是代码 -

var app = angular.module('scrapApp', ['ngRoute']);
app.controller('httpCtrl', ['$scope', '$http',function($scope, $http) {
    $scope.search = function(){
        $http({
            method : 'GET',
          url : "http://www.booking.com/autocomplete?lang=en-us&aid=304142&term=tha"
        }).then(function mySucces(response) {
            $scope.data = response.data.city;
        }, function myError(response) {
            $scope.data = response;
        });
    };
}]);

你能帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

var app = angular.module('scrapApp', ['ngRoute']);
app.controller('httpCtrl', ['$scope', '$http',function($scope, $http) {
    $scope.data = [];
    var ajaxResponse = $http.get('http://www.booking.com/autocomplete?lang=en-us&aid=304142&term=tha');
    ajaxResponse.success(function(response, status1, headers, config) 
    {
        $scope.data = response.data.city;
    }).error(function(response) 
    {
         $scope.data = response;
    });
}]);
相关问题