weixin_33691817 2018-02-06 16:16 采纳率: 0%
浏览 93

Google放置API CORS错误

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.

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);
  });   
}

enter image description here

  • 写回答

1条回答 默认 最新

  • 狐狸.fox 2018-02-06 16:40
    关注

    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" async defer

    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);
          }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)