Google Places web api 不与 superagent 合作

Google Places web api not cooperating with superagent

我正在尝试通过 google 查询地点自动完成 api,使用以下代码段

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

使用正确的参数,代码将失败,给我一个 CORS 异常

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.

如果我切换到我的网络选项卡,我可以看到请求本身运行良好,甚至 returns 我想要的日期。

我正在使用 superagent-es6-promise。

没有为 Google 后端服务器上的 Places API Web 服务设置 CORS headers。因此,由于浏览器的 Same-Origin 政策,您将无法从客户端 JavaScript 代码调用 Places API 网络服务。

为了在客户端使用 Places JavaScript,您必须使用 Google 地图 JavaScript API 的 Places 库。地点库具有附近和文本搜索功能,还有自动完成小部件和与相应网络服务非常相似的服务。

有关详细信息,请查看文档:

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

希望对您有所帮助!