Google放置API CORS错误 [英] Google places API CORS error

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

问题描述

我有一个函数,其中使用AJAX来调用Google Places API,但这会返回CORS错误.该API既是客户端API,又是服务器API,因此该API确实允许CORS,但仍然出现错误.不支持JSONP.

I have a function in which I am using AJAX to make a call to the Google Places API, but this is returning a CORS error. The API is both a client and server API, thus the API does allow CORS, but I'm still getting an error. JSONP is not supported.

我在我的代码中的其他地方进行了完全一样的AJAX调用,该调用对Geocode端点进行了调用,并且运行良好.尝试使用Places端点时,我只会收到CORS错误.

I have a AJAX call exactly like this elsewhere in my code that makes a call to the Geocode endpoint and it's been working fine. I only get the CORS errors when trying to use the Places endpoint.

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&libraries=places" async defer>

function getPlaceDetails(placeid) {
  $.ajax({
    url: 'https://maps.googleapis.com/maps/api/place/details/json?placeid='+placeid+'&key=[MY_KEY]'
  }).done(function(res) {
    console.log(res);                
  }).fail(function(err) {    
    console.log('Error: ' + err.status);
    console.log(err);
  });   
}

推荐答案

如@geocodezip所述,与其进行另一个AJAX调用,不如使用已经包含的places库.只需确保它包含在查询字符串中即可,例如:

As @geocodezip mentioned, rather than making another AJAX call, use the places library already included. Just make sure it's included in the query string like:

src ="https://maps.googleapis.com/maps/api/js?key= [MY_KEY]& libraries = places "异步延迟

src="https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&libraries=places" async defer

然后,您将可以使用.getDetails().

Then you'll have .getDetails() available for use.

    var request = {
          placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
        };
    service = new google.maps.places.PlacesService(map);
    service.getDetails(request, callback);

    function callback(place, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        createMarker(place);
      }
    }

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

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