에 방향 API를지도에서 JSON 데이터를 인쇄하는 방법. 내가 만든 XHR 객체에는 변수의 데이터가 포함되어 있지만 xhr.status는 0으로 표시됩니다.하지만 콘솔에서 XHR 객체 상태를 열었을 때 200입니다. 지도 API에서 반환되는 전체 데이터를 정확하게 인쇄하려고합니다. 심지어 CORS 요청을 사용할 수 있습니다.구글은 HTML 페이지
<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()'이후 함수 호출 범위 밖에서 그것을 호출 할 수 있다는 것을 놀랍습니다 ... –