使用$ .getJSON从google的geocode api获取数据,返回'Uncaught SyntaxError'

时间:2012-11-30 11:17:06

标签: jquery json google-maps geocode

我收到了“未捕获的SyntaxError:意外的令牌:”错误。

为什么以及如何处理此错误?

谢谢!

$.getJSON("http://maps.google.com/maps/api/geocode/json?latlng=39.971277199999996,116.4864269&language=zh-CN&sensor=false&callback=?");

您可以打开此页面并查看控制台:http://jsbin.com/ugoraw/1

像这样返回json,

{
   "results" : [
Uncaught SyntaxError: Unexpected token :
      {
         "address_components" : [
            {
               "long_name" : "16号",
               "short_name" : "16号",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "将台西路",
               "short_name" : "将台西路",
               "types" : [ "route" ]
            },
            ... ...

1 个答案:

答案 0 :(得分:1)

问号导致问题。

您需要将?更改为函数:

$(function(){
  $.getJSON("http://maps.google.com/maps/api/geocode/json?latlng=39.971277199999996,116.4864269&language=zh-CN&sensor=false&callback=handleMaps");

  function handleMaps(data) {
        alert("working");
  }

});

请注意,由于访问控制限制,您不能在jsbin中执行此操作。

我还建议您阅读以下问题和答案: jQuery and Google Maps json response