2015-09-12 111 views
1

我正在使用jQuery获取google地方API的获取请求,并且我获得了成功的响应,但数据未显示在我的页面上。这是我用来发送请求的代码,并且包含了2个开发工具向我展示的图片。Google Places API请求中的错误

$('#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

+0

你试过'dataType:'json''吗? – Lauromine

回答

0

Google Places place details API page,它看起来像服务器不支持JSONP。此外,API页面可以这样说:

Google Places API Web服务用于服务器应用程序。如果您正在构建客户端应用程序,请查看Google Places API for AndroidPlaces Library in the Google Maps JavaScript API

这使我相信CORS头可能不会被设置,因为谷歌希望你在服务器端而不是在客户端使用这个API。

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

+0

我也在尝试使用AJAX/JSON时遇到原始错误。服务器端解决方案会是什么样子? – user2586455

+0

@ user2586455任何服务器端语言都应该能够正常执行请求,而不会大惊小怪。 –