getJSON Uncaught SyntaxError:意外的令牌:

时间:2012-07-11 09:53:30

标签: javascript jquery ajax json

我知道有很多相同的问题都有答案,但我似乎无法解决我的问题。

我使用的代码:

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + results[0].geometry.location.lat() + "," + results[0].geometry.location.lng() + "&sensor=false&callback=?", function (data) {
    if (data[0]) {
        alert(data[0].formatted_address);
    }
});     

当我转到网址但不在我的应用程序中时,我可以获得json结果。如果我删除&callback=?我得到XmlHttpRequest错误,但如果我这样做,我会得到标题错误。我从一些答案中读到,我实际上应该期待一个json的格式如({json-content})而不是{json-content}。如果是这样,我如何获得正确的格式?

修改

JSON输出:

{
   "results" : [
     Uncaught SyntaxError: Unexpected token :
  {
     "address_components" : [
        {
           "long_name" : "81",
           "short_name" : "81",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Grange Rd",
           "short_name" : "Grange Rd",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "81 Grange Rd, Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248649",
           "short_name" : "248649",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Orchard",
           "short_name" : "Orchard",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248649",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.30441750,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3030750,
           "lng" : 103.8242460
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304591980291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301894019708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248643",
           "short_name" : "248643",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "River Valley",
           "short_name" : "River Valley",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248643",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.3032470,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3021720,
           "lng" : 103.8244280
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304006730291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301308769708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tanglin, Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        },
        "location" : {
           "lat" : 1.29907550,
           "lng" : 103.8164260
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        }
     },
     "types" : [ "sublocality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.3520830,
           "lng" : 103.8198360
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "country", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.28943650,
           "lng" : 103.84998020
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "locality", "political" ]
  }
   ],
   "status" : "OK"
}

JSON链接地址:

http://maps.googleapis.com/maps/api/geocode/json?latlng=1.302939,103.82309600000008&sensor=false&callback=jQuery17208982370237354189_1341999317106&_=1341999317754

1 个答案:

答案 0 :(得分:2)

此api上对JSONP的支持已被删除 - 这意味着它返回JSON而不是JSONP - 这就是您收到该错误的原因

Duplicate answer here gives a good alternative或类似的东西:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var latlong = new google.maps.LatLng("37.5097258429375","-122.27783203125");

geocoder.geocode({
    'latLng': latlong
}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].formatted_address);
    }
});​
</script>

Working example here

API docs are here