0

i got a "Uncaught SyntaxError: Unexpected token :" error.

why and how to handle this error?

thanks!

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

you can open this page and see console: http://jsbin.com/ugoraw/1

the return json like this,

{
   "results" : [
Uncaught SyntaxError: Unexpected token :
      {
         "address_components" : [
            {
               "long_name" : "16号",
               "short_name" : "16号",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "将台西路",
               "short_name" : "将台西路",
               "types" : [ "route" ]
            },
            ... ...
j0k
  • 22,600
  • 28
  • 79
  • 90
Robin
  • 634
  • 1
  • 7
  • 18
  • You get the error message text right in the middle of the JSON string? In a string generated by Google? – Álvaro González Nov 30 '12 at 11:53
  • thanks attention. here is a demo: http://jsbin.com/ugoraw/1/edit – Robin Nov 30 '12 at 13:46
  • possible duplicate of [jQuery getJSON won't work on cross domain](http://stackoverflow.com/questions/6557864/jquery-getjson-wont-work-on-cross-domain) – pimvdb Nov 30 '12 at 14:12

1 Answers1

1

The question mark is causing the problem.

You need to change the ? to a function:

$(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");
  }

});

Note you won't be allowed do this in jsbin because of access control restrictions.

I would also recommend reading the following question and answers: jQuery and Google Maps json response

Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63