我真的不明白我该怎么做:

var requestURL = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAW4CQp3KxwYkrHFZERfcGSl--rFce4tNw';

console.log(requestURL);

$.getJSON( requestURL, function( data ) {
  // data
  console.log(data);
});

and my HTML file:

  <body>

        <script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
        <script src="main.js" charset="utf-8"></script>   
  </body>

I always get the No 'Access-Control-Allow-Origin' header is present on the requested resource. message... even though if I go to https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyAW4CQp3KxwYkrHFZERfcGSl--rFce4tNw in my browser I get the proper JSON returned.

I am lead to believe that CORS can help me here. I don't understand CORS. Please, can anyone help me in plain simple terms? What should I change to make this work??

谢谢

推荐答案

You are trying to use the Google Places API Web Service on client side whereas it is designed for server side applications. That's probably why appropriate CORS response headers are not set by the server.

Notes at the beginning of the Place Details documentation中所述,您应该在Google Maps JavaScript API中使用Places Library:

如果您正在构建客户端应用程序,请查看Google Places API for AndroidGoogle Places API for iOSPlaces Library in the Google Maps JavaScript API.

Note: you will need to enable the Google Maps JavaScript API in your Google Developer Console first.

以下是一种获取地点详细信息的方法(基于example from the documentation):

<head>
    <script type="text/javascript">
        function logPlaceDetails() {
          var service = new google.maps.places.PlacesService(document.getElementById('map'));
          service.getDetails({
            placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
          }, function (place, status) {
            console.log('Place details:', place);
          });
        }
    </script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAW4CQp3KxwYkrHFZERfcGSl--rFce4tNw&libraries=places&callback=logPlaceDetails"></script>
</head>
<body>
    <div id="map"></div>
</body>

Google Place details output to the console

Json相关问答推荐

JOLT转换过滤出特定值/对象

Vega-Lite:文本笔画在外部

使用Jolt将字符串数组转换为JSON对象数组

如何在VegaLite中应用Times New Roman,CaliBiri字体

时间序列的Vega Lite分组条形图

如何在Swift中使用JSON编码器的泛型

将boost::beast::multibuffer转换为std::istream

如何在JQ中展平多维数组

在 CodePipeline 中调用 lambda 时传递用户参数

使用 JQ 从文件中删除重复的 JSON 块

使用 jq 同时迭代数组

如何删除 django jsonfield 中的特定项目

Swift - 将图像从 URL 写入本地文件

没有很多类的 GSON 解析

Laravel5 Json 获取文件内容

在 JavaScript 中从 Json 数据中删除反斜杠

Django - 异常处理最佳实践和发送自定义错误消息

如何将 MongoDB 查询转换为 JSON?

Javascript:如何判断 AJAX 响应是否为 JSON

Laravel 5 控制器将 JSON 整数作为字符串发送