hem*_*ter 3 jquery google-places-api
Google Places API现已普遍推出.我试图在jQuery中使用.ajax()调用来调用Google Places.我一直回来的错误是Uncaught SyntaxError:意外的令牌:
我正在使用jQuery 1.5.2.我也尝试了1.5.1,但结果相同.如果我能提供帮助,我宁愿不转向1.6.1.
从那以后我就把这样的ajax调用到其他API,但是我在使用Google Places时遇到了麻烦.以下是您可以使用的基本代码示例.您需要在Google控制台上提供自己的密钥(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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
从我可以从文档看,谷歌的地方Web服务API并没有支持JSONP -只JSON.您看到的错误是因为响应只是JSON,但正在被解析,好像它是JSONP并导致错误.
查看Google Maps JavaScript API - 它包含您可以使用的Places库 - 请参阅google.maps.places.PlacesServices#search().
AFAIK,似乎正在转向删除JSONP支持 - 例如,Geocoding API用于支持v2中的JSONP(未记录)但不再支持v3.有人建议可能是为了鼓励开发人员使用JavaScript API.