Ich möchte einen Api Aufruf zu einem JSON (https://maps.googleapis.com/maps/api/geocode/json?address=Pune), indem Sie ihm einen Stadtnamen (hart codiert) und erhalten die Breite und Länge im Gegenzug. Ich habe den unten angegebenen Code versucht.Javascript json api call
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Pune", true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
};
xobj.send();
}
function init() {
loadJSON(function(response) {
// Parse JSON string into object
var myObj = JSON.parse(this.responseText);
alert(myObj.results[0].geometry.bounds.northeast.lat);
alert(myObj.results[0].geometry.bounds.northeast.lng);
alert(myObj.results[0].geometry.bounds.southwest.lat);
alert(myObj.results[0].geometry.bounds.southwest.lng);
alert(myObj.results[0].geometry.location.lat);
alert(myObj.results[0].geometry.location.lng);
alert(myObj.results[0].geometry.viewport.northeast.lat);
alert(myObj.results[0].geometry.viewport.northeast.lng);
alert(myObj.results[0].geometry.viewport.southwest.lat);
alert(myObj.results[0].geometry.viewport.southwest.lng);
});
}
Sie einige Code geteilt haben, aber Sie haben nicht beschrieben, welches Problem Sie haben. Gibt es einen Fehler? Siehst du die Ausgabe? Ist die Ausgabe anders als erwartet? – smarx
Sieht auch so aus, als ob du eine schließende geschweifte Klammer für 'loadJSON' verpasst hast. Ist das der genaue Code, den Sie ausführen möchten? – smarx
Ihr 'xobj.send()' ist auch an der falschen Stelle. – smarx