You are trying to use JSONP here.
Jsonp
If the URL contains the string "callback =?" (or similarly, as defined by the server-side API), requests> are processed as JSONP. See the discussion of jsonp data type in $ .ajax () for more details.
Important. Starting with jQuery 1.4, if the JSON file contains a syntax error, the request usually fails.
See: http://api.jquery.com/jQuery.getJSON/
But the URL you are calling returns plain JSON, so the parsing fails with the syntax error and getJSON fails.
Now, when you try to change the geocode URL to use JSONP, you get a 404 error, since Google recently removed JSONP support:
In short:
You cannot just use the geocode api from a JavaScript browser, you will have to add a proxy script to your server.
And even if the request works, there is still an error in your code:
json.results is an array of results, so it does not have a geometry property.
You need to access the first element of the array to get to the actual object that has the geometry property:
json.results[9].geometry.location.lng
source share