Google Places API cors [英] Google Places API cors

查看:77
本文介绍了Google Places API cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的localhost机器获取get请求(我的应用程序将保留在localhost上)以获取有关某个地方的一些信息。

I'm trying to do a get request from my localhost machine(And my app will stay on localhost) to get some information about a place.

但是,由于CORS,Google Places API和/或我的Chrome将不允许我执行此请求( XMLHttpRequest无法加载https://maps.googleapis.com/maps/api/place/details/json? placeid = [placeid]& key = [key]。请求的资源上没有'Access-Control-Allow-Origin'标题。因此不允许原点'http:// localhost:8080'访问。)。

However, the Google Places API and/or my Chrome won't let me do this request due to CORS (XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/details/json?placeid=[placeid]&key=[key]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.).

是否有某种方式(没有CORS Chrome插件)可以从Google Places API获取访问权限?

Is there some way (Without a CORS Chrome plugin) to get access from the Google Places API?

我只是在Javascript中使用Vue2&的简单GET请求。 Vue资源:

I'm just using a simple GET request in Javascript with Vue2 & Vue Resource:

this.$http.get(`https://maps.googleapis.com/maps/api/place/details/json?placeid=${googleplacesPlace}&key=${googleplacesToken}`).then(response => {
})


推荐答案

对我来说同样的问题,用谷歌地图Javascript API解决这个问题:而不是使用直接的api网址接受CORS,使用谷歌地图的instene(新的google.maps.places ...)

Same issue for me, solved it with the Google Maps Javascript API this way :Instead of using the direct api url with does not accept CORS, use instene of google map (new google.maps.places...)

 var request = {
  location: new google.maps.LatLng(lat, lng), // gogle map latLng objet
  radius: '5000',
  types: ['airport']
};

let service = new google.maps.places.PlacesService(this.map);
service.nearbySearch(request, this.Fncallback);
  }

 Fncallback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      var place = results[i];
  }
 }
}

这篇关于Google Places API cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆