XMLHttpRequest
javascript에서 요청을 보내던 옛날 방식
promise를 지원하지 않아서 많은 콜백이 있음
설정방식 복잡함
지금은 잘 쓰이지 않음
const url = "https://swapi.dev/api/people/1/";
//XMLHttpRequest를 new키워드를 사용해서 객체 생성하고 변수에 할당
const req = new XMLHttpRequest();
//req.open 메소드를 사용해서 (사용할 http verb, apiUrl )
req.open("GET", url);
//send 메소드를 사용해줌
req.send();
//요청이 됐을 때 실행할 함수
req.onload = function () {
console.log("ggg");
//this는 받아온 json 데이터 우리가 필요한 것은 responseText임
//JSON.parse메소드로 객체로 변환 변수에 할당
let data = JSON.parse(this.responseText);
//이 안에서 작업을 해야함
console.log(data);
console.log(data.name);
};
//에러가 있을 때 실행할 함수
req.onerror = function () {
console.log("ggg");
console.log(this);
};