我想在地圖 api的控制檯中打印狀態和其他變量。 我創建的XHR對象包含變量中的數據,但是當我打印 xhr.status時,它表示0。但是在控制檯中當我打開XHR對象時,狀態爲 200. 我完全想要打印從maps API返回的所有數據。我甚至啓用了CORS請求 。如何在google頁面上打印來自Google maps方向api的JSON數據
<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);
}
那麼從代碼我很驚訝,你甚至可以稱之爲你的'console.log()' - 它在函數範圍之外... –