
특징
장점
단점
예제
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts", true);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send();
Promise 기반으로 설계되어 더 간결하고 읽기 쉬운 코드 작성이 가능.Promise를 지원하여 비동기 처리가 간편.Promise가 resolve됨. (상태 코드 확인 필요)AbortController 없이 타임아웃 제어 불가.polyfill이 필요.fetch("https://jsonplaceholder.typicode.com/posts")
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => console.log(data))
.catch((error) => console.error("Fetch error:", error));특징
장점
CancelToken 또는 AbortController 사용 가능.단점
예제
import axios from "axios";
axios
.get("https://jsonplaceholder.typicode.com/posts")
.then((response) => console.log(response.data))
.catch((error) => console.error("Axios error:", error));
