jQuery getJSON won't work on cross domain
I'm trying to use google maps' REST api to convert an typed in adress to coordinates. To do so I use jQuery's getJSON function as it uses a native JSONP call.
Though it won't work, it just doesn't get to success, so the alert is never called.
$("#makeCords").click(function(){
straat = $('#straat').val();
stad = $('#Stad').val();
land = $('#Country').val();
if(straat == "" || stad == "" || land == ""){
alert("Onvoldoende gegevens! Vul straat, stad en land aan voor het gebruik van de ´bereken coordinaten´ functie.");
}else{
$.getJSON("http://maps.google.com/maps/api/geocode/json?callback=?",
{
adress: straat +", " + stad +", " + lan开发者_运维问答d,
sensor: "false"
},
function(data) {
alert(data);
});
}
});
The Google Maps Geocoding API does not support JSON-P; it will ignore your callback
parameter, thus cross-domain JSON-P won't work.
You should use official Google Maps Javascript library instead. Internally the library does use JSON-P to pass the information (since it's almost the only way to do cross-domain requests), but that URL is reserved to official library use only.
try adding &format=json to your get jason url and chaneg &callback=? to &jsoncallback=?
Try jquery jsonp:
$.ajax({
type: "GET",
url: "http://url",
dataType: "jsonp" ,
success: function (data) {
}
});
Isn't it a kind of security that javascript (in this case jquery) won't ever get or post data outside it's own domain (at least as far as I know javascript is not allowed to post
or get
cross domains)? You could use a local php script gathering the json information from the google server by curl and send your get-query to this local script. I would give this workaround a try...
Another way would be to use the google maps api for javascript. You could use the gecoding functions from that api to retrieve the latitude and longitude from a given address. So you wouldn't need to make any gets or posts...
精彩评论