问题描述
|
由于任何原因,此代码似乎不起作用
var request = \"https://maps.googleapis.com/maps/api/place/search/json?\";
request = request + \"location=\"+lat+\",\"+lng+\"&\";
request = request + \"radius=500&\";
request = request + \"types=&\";
request = request + \"name=&\";
request = request + \"sensor=false&\";
request = request + \"key=\"+key;
$.getJSON(request,function(data){ alert(data); });
该字符串有效,并且只要将其加载到浏览器中就可以得到结果。您在这里看到任何问题吗?
编辑:好的,问题解决了。 Google Places API实际上不接受Ajax jsonp调用。我将不得不使用其JavaScript API。有关更多详细信息,请参见此线程:
如何使用Google地方信息获取位置名称数组?
多谢您的回覆。因此,我现在使用以下代码进行API调用:
$.ajax({
url: \"https://maps.googleapis.com/maps/api/place/search/json\",dataType: \'jsonp\',data: {
location:lat+\",\"+lng,radius:500,types:\"\",name:\"\",sensor:\"false\",key:key
},success: function(data) {
alert(data);
}
});
Whiich没有显示任何警报框。通过检查代码,我捕获到一条错误消息:
\"html_attributions\" : [],json:2Uncaught SyntaxError: Unexpected Token :
\“结果\”:[
{
但是,在此错误消息之后,在控制台中,请求的数据实际上显示在“结果”字段中。
解决方法
Google Places API不支持JSONP请求。