fetch(url, options)
.then(response => response.json())
.then(data => {
// ์๋ต ๋ฐ์ดํฐ ์ฒ๋ฆฌ
console.log(data);
})
.catch(error => {
// ์๋ฌ ์ฒ๋ฆฌ
console.error(error);
});
url
์์ฒญ์ ๋ณด๋ผ URL ์ฃผ์๋ฅผ ์
๋ ฅ
options
์์ฒญ์ ๋ํ ์ต์
์ ์ค์ ํ ์ ์๋ ๊ฐ์ฒด
HTTP ๋ฉ์๋, ํค๋, ๋ณธ๋ฌธ ๋ฐ์ดํฐ ๋ฑ์ ํฌํจํ ์ ์์
.then()
์๋ฒ๋ก๋ถํฐ ์๋ต์ด ์ค๋ฉด ์คํ๋๋ ์ฝ๋ฐฑ ํจ์
์๋ต ๊ฐ์ฒด๋ฅผ ๋ฐ์
.then()
๋ ๋ฒ์งธ .then() ๋ฉ์๋์์๋ ์๋ต ๊ฐ์ฒด์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌ
response.json()์ ์ฌ์ฉํ์ฌ JSON ๋ฐ์ดํฐ๋ก ๋ณํ
.catch()
์์ฒญ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ์คํ๋๋ ์ฝ๋ฐฑ ํจ์
async function fetchData() {
try {
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
fetchData();
์ถํ์ axios๋ ๋น๊ต๋ฅผ ํด๋ด์ผ ๊ฒ ๋ค.