用javascript解析google地方json [英] parse google place json with javascript

查看:135
本文介绍了用javascript解析google地方json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于任何原因,此代码似乎无效

for any reason this code doesn't seem to work

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); });

字符串是有效的,如果我在浏览器中加载它,我会得到结果。你看到这里有什么不对吗?

The string is valid and i get the result if i just load it in my browser. Do you see anything wrong here?

编辑:好的,问题解决了。 Google Places API实际上不接受ajax jsonp调用。我将不得不使用他们的JavaScript API。有关详情,请参阅此主题:

alright, problem solved. The Google Places API actually does not accept ajax jsonp calls. I'll have to use their javascript API instead. See this thread for more details:

How do I use Google Places to get an array of Place names?

感谢您的回复。所以我现在用以下代码进行API调用:

Thanks for your replies. So I now make the API call with the following code:

    $.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不显示任何警报框。通过检查代码,我收到了错误消息:

Whiich doesn't show any alert box. By inspecting the code, I catch an error message:

   "html_attributions" : [],
  json:2Uncaught SyntaxError: Unexpected Token :

results:[
{

"results" : [ {

但是,在控制台中出现此错误消息后,请求的数据实际显示在结果字段中......任何想法?

However, following this error message, in the console, the requested data actually shows in the "results" field... Any idea?

推荐答案

Google Places API不支持JSONP请求。

The Google Places API doesn't support JSONP requests.

这篇关于用javascript解析google地方json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆