javascript - Google Places API 请求出错

标签 javascript jquery api get google-api

我正在使用 jQuery 向 google place API 发出 get 请求,并且得到了成功的响应,但数据未显示在我的页面上。这是我用来发送请求的代码,并且我提供了 2 张开发工具向我展示的图片。

$('#button').click(function () {
    $.ajax({
        type: 'GET',
        dataType: 'jsonp',
        url: "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJh4vtq-kQsYkRET-Imf6RsuA&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        success: function (data) {
            $("#over_map").text(data.formatted_address);
        }
    });
});

开发工具截图:

enter image description here

enter image description here

最佳答案

来自Google Places place details API page ,看来服务器不支持JSONP。此外,API 页面有这样的说法:

The Google Places API Web Service is for use in server applications. If you're building a client-side application, take a look at the Google Places API for Android and the Places Library in the Google Maps JavaScript API.

这让我相信 CORS header 可能不会被设置,因为 Google 希望您在服务器端而不是客户端使用此 API。

我的建议是尝试将 AJAX 请求中的 dataType: 'jsonp', 更改为 dataType: 'json',,如果这会导致浏览器抛出异常源错误,那​​么您将需要制定服务器端解决方案,而不是当前的客户端解决方案。

关于javascript - Google Places API 请求出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32534041/

相关文章:

python - 努力通过 Python 理解 Guardian API

javascript - 正则表达式将文件夹添加到所有图像src

javascript - 如何获取数组A和B中的所有项目并将它们保存到数组C中?使用 javascript 而不使用循环

javascript - jQuery 居中 div 到窗口垂直调整大小问题

ios - 如何从服务器端确认客户端 Facebook 应用程序登录

javascript - REST:调用 javascript 函数将操作发送到 API

javascript - vuetify v-bind 内联样式值不更新

javascript - 在不同的 .js 文件之间传递变量;一个在 iframe 中

Javascript 将 onclick 事件添加到窗口加载时的单选按钮

javascript - 禁用表格中的行点击