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" ]
            },
            ... ...
3
  • You get the error message text right in the middle of the JSON string? In a string generated by Google? Commented Nov 30, 2012 at 11:53
  • thanks attention. here is a demo: jsbin.com/ugoraw/1/edit Commented Nov 30, 2012 at 13:46
  • possible duplicate of jQuery getJSON won't work on cross domain Commented Nov 30, 2012 at 14:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.