【问题标题】:Getting empty response from http get request从 http get 请求获取空响应
【发布时间】:2016-09-14 15:42:05
【问题描述】:

我使用 MEAN 堆栈开发了一个 Web 应用程序。 Nodejs 用于创建服务器和连接 Mongodb。我需要在前端显示来自这个 url“http://www.booking.com/autocomplete?lang=en-us&aid=304142&term=tha”的 json 数据,然后将其添加到 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;
        });
    };
}]);

你们能帮我解决问题吗?

【问题讨论】:

  • 是“请求的资源上不存在'Access-Control-Allow-Origin'标头。”开发者工具的控制台出现异常?
  • 不,我认为它是响应标头,但不确定。其实我对CORS了解不多。

标签: angularjs json node.js


【解决方案1】:

试试这个:

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;
    });
}]);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 2021-06-04
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多