【问题标题】:VueJS axios CORS Google ApiVueJS axios CORS Google Api
【发布时间】:2018-06-03 19:09:20
【问题描述】:

我尝试从 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)
    })                                                                                              

然后我有这个错误 我知道服务器端 CORS 的错误,但我该怎么办?

【问题讨论】:

标签: google-maps-api-3 vue.js cors


【解决方案1】:

在我的项目中,我使用 NuxtJS 并找到了解决方案。 您必须在服务器端(nodeJS)而不是客户端发出请求。 如下所示,方法 fetch 允许您执行此操作,并且有效
https://nuxtjs.org/guide/vuex-store#the-fetch-method

【讨论】:

【解决方案2】:

只需在您自己的服务器上创建和调用网络服务,然后再调用谷歌的网络服务。

您的 web 服务的主要代码在 PHP 中可能如下所示:

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

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2018-04-27
    • 2021-06-17
    • 2021-03-01
    • 2021-10-23
    • 2020-01-02
    • 2021-04-11
    • 2018-08-04
    • 2022-12-01
    相关资源
    最近更新 更多