【问题标题】:Google Places web api not cooperating with superagentGoogle Places web api 不与 superagent 合作
【发布时间】:2018-02-11 22:28:23
【问题描述】:

我正在尝试通过谷歌查询地点自动完成 api,使用以下 sn-p

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.

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

我正在使用 superagent-es6-promise。

【问题讨论】:

标签: reactjs google-maps cors google-places-api superagent


【解决方案1】:

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

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

更多详情请查看文档:

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

希望对您有所帮助!

【讨论】:

  • 原来我已经通过 google-map-react 加载了这些库,我只需要扩展它就可以访问 places API。
猜你喜欢
  • 2015-11-08
  • 1970-01-01
  • 2016-04-02
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
相关资源
最近更新 更多