Google Places web api not cooperating with superagent

975 Views Asked by At

I'm trying to query the places autocomplete api by google, using the following snippet

request.get(`https://maps.googleapis.com/maps/api/place/autocomplete/`).query(params)

With the correct params the code will fail, giving me a CORS exception

Failed to load https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amsterdam&key={super secret key}: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7000' is therefore not allowed access.

If I switch to my network tab, I can see that the request itself is working perfectly fine and that it even returns the date I want it to.

I'm working with superagent-es6-promise.

1

There are 1 best solutions below

1
On BEST ANSWER

The CORS headers are not set for Places API web service on Google backend servers. So you won't be able to call Places API web service from the client side JavaScript code due to the Same-Origin policy of the browsers.

In order to use Places on client side JavaScript you have to use a Places library of Google Maps JavaScript API. The places library has nearby and text search functionality, also autocomplete widget and service very similar to the corresponding web service.

For further details please have a look at the documentation:

https://developers.google.com/maps/documentation/javascript/places

Hope it helps!