我正在使用 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);
}
});
});
开发工具截图:
最佳答案
来自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/