Google Places API 现已全面推出。我正在尝试使用 jQuery 中的 .ajax() 调用来调用 Google Places。我不断返回的错误是Uncaught SyntaxError: Unexpected token :
我正在使用 jQuery 1.5.2。我也尝试过 1.5.1,但结果相同。如果可以的话,我宁愿不迁移到 1.6.1。
从那时起,我就对其他 API 进行了这样的 ajax 调用,但是在使用 Google Places 时遇到了问题。以下是您可以使用的非常基本的代码示例。您需要在 Google 提供的 API 控制台 (https://code.google.com/apis/console) 中获取自己的密钥
jQuery.noConflict();
jQuery(document).ready(function(){
var url = 'https://maps.googleapis.com/maps/api/place/search/json';
jQuery.ajax({
url: url,
dataType: 'jsonp',
type: 'GET',
data: {
location: '33.787794,-117.853111',
radius: 1000,
name: 'coffee',
key: 'your_key', // add your key here
sensor: 'false'
},
// on success
success: function(data, textStatus, jqXHR){
console.log(data);
},
// on failure
error: function (jqXHR, textStatus, errorThrown){
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
});