首页 > 解决方案 > Google Places API 返回错误:使用 Axios 时出现网络错误

问题描述

使用 python 请求并发送以下内容时

outs = requests.get("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=32.78257453407587,-117.11499507124117&radius=15000&keyword=Paddle+Board+Rentals&key=MY_KEY")

我得到一个状态 200 和结果。

当我使用 Axios 时

    async getPlaces(point, radius, keywords, type=""){
        const url = this.urlBuilder(point, radius, keywords, type);
        const response = await axios.get(url)
            .then(res => {return res})
            .catch(err => {return err});
        return response
    }

和上面的python中的url完全相同,我得到了

Error: Network Error

python中是否有可能导致两个请求之间存在差异的默认标头?

编辑:使用 console.log 我能够得到这个错误

错误:禁止跨域 http://localhost

因此,使用 fetch 或 axios 运行 google places 请求存在问题,因为它们来自本地主机。

标签: javascriptaxiosgoogle-places-api

解决方案


问题是谷歌不允许用户使用 javascript 通过 https 进行这些网络调用。您必须使用 google places api。


推荐阅读