2016-03-04 89 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「; –

相關問題