Ich möchte den Status und andere Variablen in der Konsole der Karten API drucken. Das XHR-Objekt, das ich erstellt habe, enthält die Daten in Variablen, aber wenn ich xhr.status drucken, heißt es 0. Aber in der Konsole Wenn ich geöffnet XHR Objektstatus ist 200. Ich möchte genau die gesamten Daten von Karten API zurückgeben. Ich sogar aktiviert CORS-Anfrage.So drucken Sie JSON-Daten aus Google Maps Richtungen API auf HTML-Seite
<button type="button" onclick="myFunction()">Submit</button></br>
</form>
<p id="demo"> hello</p>
<script>
function myFunction() {
var slat = document.getElementById("slat").value;
var slong = document.getElementById("slong").value;
var dlat = document.getElementById("dlat").value;
var dlong = document.getElementById("dlong").value;
//xhr object for sending request to maps api
var xhr=
createCORSRequest('GET',"https://maps.googleapis.com/maps/api/directions/json?
origin=75+9th+Ave+New+York,+NY&destination=Boston&key=My key was here");
if (!xhr) {
throw new Error('CORS not supported');
}
console.log(xhr); // seeing the xhr object
console.log(xhr.response); // trying to print xhr response but nothing is coming
console.log(xhr.status); // 0 is being displayed as status but 200 is there in xhr object
console.log(xhr.responseType);
}
Nun von dem Code ich bin überrascht, Sie können es sogar aufrufen, da Ihre 'console.log()' - es außerhalb des Funktionsumfangs ... –