Sergey Andreev
Sergey Andreev

Reputation: 1488

VueJS axios CORS Google Api

I try to get data from API Google Places

axios.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=...&key=mykey')
    .then(response => {
      commit('SET_REVIEWS', { list: response.data })
    }, error => {
      console.log(error)
    })                                                                                              

And after that i have this error enter image description here I know the error with CORS on server side, but what should i do?

Upvotes: 1

Views: 2964

Answers (2)

Sergey Andreev
Sergey Andreev

Reputation: 1488

In my project I use NuxtJS and I found the solution. You have to make a request on the server side (nodeJS), not the client. As shown below, the method fetch allows you to do this and it works
https://nuxtjs.org/guide/vuex-store#the-fetch-method

Upvotes: 2

rosell.dk
rosell.dk

Reputation: 2472

Simply create and call a webservice on your own server which in turn calls googles webservice.

The main code of your webservice could look like this, in PHP:

$url = 'https://maps.googleapis.com/maps/api/place/details/json?key=' . $key . '&placeid=' . $place_id; echo json_decode(file_get_contents($url), true);

Upvotes: 0

Related Questions