CORS e Angular $ http GET para a API do Google Places
Novo no angular, tentando configurar um aplicativo muito simples usando a API do Google Maps / Places.
Posso usar com êxito o serviço de geolocalização com:
.controller('MainCtrl', [
'$scope',
'$http',
function($scope, $http){
$scope.userLocation = [];
$scope.currentLocation = function() {
$http({
method: 'GET',
url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=xxx'
}).then(function successCallback(response) {
$scope.userLocation.push(response);
});
}
}])
Problema: Quando tento uma solicitação GET semelhante à API do Google Places, recebo erros CORS:
XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=36.23…5.1458888&type=bar&key=xxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:3000' is therefore not allowed access.
Pergunta, questão: Posso fazer esses tipos de solicitações da Angular ou preciso configurar o back-end para fazer essa chamada?
Tentei usar JSONP como método, que passou o problema do CORS, mas tenho o problema de não conseguir acessar os dados. Não tenho certeza se é JSON inválido ... mas dizUnexpected identifier :
, e quando visualizo a fonte, parece-me perfeitamente com dados excelentes:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 36.2388477,
"lng" : -115.1540073
},
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "578bfba1f17b0c5fbdafd5b71600b50d3e95c1ca",
"name" : "Ruby Tuesday",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},