2016-03-04 80 views
1

我试图发送一个请求在我的angularjs应用程序到谷歌API来获取在一个位置周围的半径景点。但是我在chrome中收到此错误消息: 请求的资源上没有“Access-Control-Allow-Origin”头。因此不允许访问原产地'http://localhost:9000'。当发送请求到谷歌API时访问控制允许来源

我试过json和jsonp,但结果是一样的。 :S

$http.get('https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&type=museum&key=<MYKEY>') 
     .then(function(response) { 
      $scope.dataResult = response.data; 
     }); 
    $scope.getlocation = function() { 

     $scope.method = 'GET'; 
     $http({ method: $scope.method, url: "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&type=museum&key=<MYKEY>&callback=JSON_CALLBACK" }). 
     success(function(data, status) { 

      $scope.status = status; 
      $scope.data = data; 
      console.log(data); 
     }). 
     error(function(data, status) { 
      $scope.data = data || "Request failed"; 
      $scope.status = status; 
     }); 

    }; 

我不想在地图上显示结果,我只希望列表格式。

+0

问题是你的起源是'http',你的目的地是'https'。让这两者平等。 – georgeawg

回答

0

这是CORS问题,从客户端您可以启用这样的Cors请求。

angular.module('yourapp').config(['$httpProvider', function($httpProvider) { 
     $httpProvider.defaults.useXDomain = true; 
     delete $httpProvider.defaults.headers.common['X-Requested-With']; 
    } 
]); 

与你的情况的问题是,https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&type=museum&key=<MYKEY>不允许你的出身有机会获得响应。因此你无法阅读它。

我希望你有一个有效的密钥,同时注册你已经列出你的本地主机的地方...因为谷歌需要知道这些。

+0

我已经启用了http:// localhost,http:// localhost:9000 /#/和http:// localhost:9000到密钥。如果我查看Google Places API Web服务,则看到每次请求都会增加请求。并且服务api也被启用。 –

+0

@JediSchmedi:你可以在你的配置中添加上面的东西,并检查它的工作与否。 – Thalaivar

+0

这是app.js 的.config(函数($ routeProvider,$ httpProvider){$ httpProvider.defaults.useXDomain = TRUE; $ httpProvider.defaults.withCredentials = TRUE; 删除$ httpProvider.defaults.headers.common [“X-Requested-With”]; $ httpProvider.defaults.headers.common [“Accept”] =“application/json”; $ httpProvider.defaults.headers.common [“Content-Type”] =“application/json“; –

相关问题