マップのコンソールでステータスとその他の変数を印刷したい場合 api。 私が作成したXHRオブジェクトには変数のデータが含まれていますが、 xhr.statusは0と表示されますが、コンソールではXHRオブジェクトステータスを開くと です。 マップAPIから返されるデータ全体を正確に印刷します。私も CORS要求を有効にしました。GoogleマップからJSONデータを印刷する方法htmlページの指示api
<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()'から関数スコープの外に呼び出すこともできます... –