0

I want to be able to make a get request with axios to the google places api with an url like the following below

https://maps.googleapis.com/maps/api/place/textsearch/json?query=pizza+&type=restaurant&location=-21.8029127,142.9766041&radius=10000&key=MYAPIKEY

But I get a CORS error.

So I've scoured to try find how to achieve this and I cant seem to find a simple solution. I don't want any maps or autocomplete functionality that the current npm libraries offer. I just want to be able to get results from the places api based on the query that is entered by the user.

jonathanpuc
  • 179
  • 3
  • 11

1 Answers1

0

If you are getting a CORS error, it means that your browser is restricting a cross-origin request originated from your application script. One solution to avoid this is by providing the CORS header. However, you do not have access to the API server to get it. So you could specify the origin in your Google Maps API call using the origin param.

https://maps.googleapis.com/maps/api/place/textsearch/json?query=pizza+&type=restaurant&location=-21.8029127,142.9766041&radius=10000&key=MYAPIKEY&origin=*

Notice that I have provided origin=*. But you could use your own DNS instead of *, in case you have one set up.

Below is a transcript from the Mozilla Web Docs website about cors:

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request HTTP resources from the same origin the application was loaded from, unless the response from the other origin includes the right CORS headers.

Weder Ribas
  • 361
  • 6
  • 16
  • Unfortunately this doesn't work. Still get the same old cors error. – jonathanpuc Jun 17 '18 at 22:03
  • @jonathanpuc Please edit your question adding more details about the error you got, the context, the code related to the API call, etc. It is easier to answer with some more data. – Weder Ribas Jun 18 '18 at 16:29