2012-07-11 65 views
1

我知道有很多与所有答案相同的问题,但我似乎无法解决我的问题。getJSON Uncaught语法错误:意外的令牌:

,我使用的代码:

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

当我去到URL,但不是我的应用程序中,我可以得到JSON结果。如果我删除&callback=?我得到XmlHttpRequest错误,但如果我这样做,我会得到标题错误。我已经阅读了一些答案,我实际上应该会期望带有像{{json-content})这样的格式的json,而不是我正在获取的{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

你可以发布你实际上得到JSON输出。或者一个完整的示例json请求地址。 – 2012-07-11 09:55:57

+0

+1向我们展示完整的示例url – WTK 2012-07-11 09:57:30

+0

@JonTaylor我刚刚添加了结果和请求地址 – consprice 2012-07-11 10:05:00

回答

2

在这个API JSONP的支持已经下降 - 这意味着它的返回JSONJSONP - 这就是为什么哟UR收到这个错误

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

+0

这是否意味着我应该使用.ajax来代替? – consprice 2012-07-11 10:03:52

+0

@consprice不,只需使用Maps API即可。 – 2012-07-11 10:04:53

+0

难道他不能摆脱&回调部分,这是jQuery使它假设的位JSONP – 2012-07-11 10:06:04

相关问题