๐ง AJAX๋?
AJAX๋ Asynchronous JavaScript and XML์ ์ฝ์๋ก
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํด์ ์๋ฒ์ ๋น๋๊ธฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ธฐ๋ฅ์ ๋งํ๋ค.
AJAX๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์ ๊ณตํ๋ XMLHttpRequest
๊ฐ์ฒด๋ฅผ ํตํด ๋์ํ๋ค.
์ด๋ฆ์ XML์ด ๋ค์ด๊ฐ ์์ง๋ง, ์ค์ ๋ก๋ XML ๋์ JSON ํ์์ ๋ ๋ง์ด ์ฌ์ฉํ๋ค.
๐ AJAX์ ๋ฑ์ฅ ๋ฐฐ๊ฒฝ๊ณผ ์ฅ์
๐ ๋ฑ์ฅ ๋ฐฐ๊ฒฝ
AJAX๊ฐ ๋ฑ์ฅํ๊ธฐ ์ด์ ์ ์นํ์ด์ง๋
์ผ๋ถ ๋ด์ฉ๋ง ๋ฐ๊พธ๊ณ ์ถ์ด๋ ์ ์ฒด ํ์ด์ง๋ฅผ ๋ค์ ๋ก๋ํด์ผ ํ๋ค.
๐ ๋ฌธ์ ์ (AJAX ์ด์ )
- ํ๋ฉด ๊น๋นก์ → ์ ์ฒด ์๋ก๊ณ ์นจ์ผ๋ก ์ฌ์ฉ์ ๊ฒฝํ ์ ํ
- ๋ถํ์ํ ๋ฐ์ดํฐ ์ ์ก → ๋ณ๊ฒฝํ์ง ์์๋ ๋๋ HTML๊น์ง ์ ์ก
AJAX๋ ์์ ๊ฐ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ฑ์ฅํ๋ค.
AJAX๋ ์๋ฒ๋ก๋ถํฐ ํ์ํ ๋ฐ์ดํฐ๋ง ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ์ ์ก๋ฐ์ ํ,
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด DOM์ ์กฐ์ํ์ฌ ํ์ด์ง์ ์ผ๋ถ๋ง ๋์ ์ผ๋ก ๊ฐฑ์ ํ๋ค.
โ AJAX์ ์ฅ์
- ๋ถ๋ถ ๊ฐฑ์ → ํ์ํ ๋ฐ์ดํฐ๋ง ๋ฐ์์ DOM์ ์กฐ์ (ํ๋ฉด ๊น๋นก์ ์์)
- ๋ฐ์ดํฐ ํจ์จ์ฑ → ๋ถํ์ํ ๋ฆฌ์์ค ๋ญ๋น ๋ฐฉ์ง
- ๋น ๋ฅธ ์ํธ์์ฉ → ๋ฐ์คํฌํฑ ์ฑ๊ณผ ์ ์ฌํ UX ์ ๊ณต
- ์ญํ ๋ถ๋ฆฌ → ์๋ฒ๋ ๋ฐ์ดํฐ ์ ๊ณต, ํด๋ผ์ด์ธํธ๋ ํ๋ฉด ๊ด๋ฆฌ์ ์ง์ค
๐ AJAX์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ฐฉ์์ ๋ฐ์
1๏ธโฃ XMLHttpRequest
XMLHttpRequest
๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ฌ์ฉํ๋๋ฐ,
์ด๋ก ์ธํด ๋น๋๊ธฐ ์์ ์ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํ ๋ ์ฝ๋ฐฑ ํฌ์ด๋ผ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ฝ๋ฐฑ ํฌ → ์ฝ๋๊ฐ ๊น๊ฒ ์ค์ฒฉ๋์ด์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๊ณ ์ ์ง๋ณด์๊ฐ ์ด๋ ค์์ง๋ ํ์
const xhr = new XMLHttpRequest();
xhr.open("GET", "/data");
xhr.onload = function () {
console.log(xhr.responseText);
};
xhr.send();
2๏ธโฃ Promise
- ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ํ(pending, fulfilled, rejected)์ ๊ฒฐ๊ณผ๊ฐ์ ๊ด๋ฆฌ
then
,catch
,finally
๋ฅผ ์ด์ฉํด ๊ฐ๋ ์ฑ ๊ฐ์fetch
๋ Promise๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์XMLHttpRequest
๋ณด๋ค ์ฌ์ฉ๋ฒ์ด ๊ฐ๋จํจ
fetch("/data")
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
3๏ธโฃ async/await
- ES2017์์ ๋์
- Promise ๊ธฐ๋ฐ ๋ฌธ๋ฒ
- ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋๊ธฐ ์ฝ๋์ฒ๋ผ ์์ฑ ๊ฐ๋ฅ → ์ง๊ด์ ์
- async ํจ์๋ ํญ์ ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํ๊ณ await๋ ํ๋ก๋ฏธ์ค๊ฐ ์ฒ๋ฆฌ๋ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํจ
- ํ๋ก๋ฏธ์ค ์ฒด์ด๋๋ณด๋ค ํจ์ฌ ๋ ๊ฐ๋ ์ฑ ๋์ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์
async function getData() {
try {
const res = await fetch("/data");
const data = await res.json();
console.log(data);
} catch (err) {
console.error(err);
}
}
4๏ธโฃ axios
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์ค์น ํ์:
npm install axios
) fetch
๋ณด๋ค ๋ฌธ๋ฒ์ด ๊ฐ๋จํ๊ณ ๋ค์ํ ํธ์๊ธฐ๋ฅ์ ์ ๊ณตํจ
โ axios ์ฅ์
1. ์๋ JSON ๋ณํ
fetch๋ ์๋ต์ JSON์ผ๋ก ๋ณํํ๋ ค๋ฉด res.json()์ ํธ์ถํด์ผ ํ์ง๋ง
axios๋ ์๋์ผ๋ก JSON์ ๋ณํํด์ค๋ค.
// fetch
async function getDataFetch() {
const res = await fetch("/data");
const data = await res.json(); // ์ง์ ๋ณํ ํ์
console.log(data);
}
// axios
import axios from "axios";
async function getDataAxios() {
const res = await axios.get("/data");
console.log(res.data); // ์๋ JSON ๋ณํ
}
2. ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ(params ์ต์ )
axios๋ params
์ต์
์ ์ ๊ณตํด์ ๊ฐ๋จํ๊ฒ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์์ฑํ ์ ์๋ค.
// fetch + URL ๊ฐ์ฒด
async function getUsersFetch() {
const url = new URL("/users", window.location.origin);
url.search = new URLSearchParams({
role: "admin",
active: true,
});
const res = await fetch(url);
const data = await res.json();
console.log(data);
}
// axios
async function getUsersAxios() {
const res = await axios.get("/users", {
params: {
role: "admin",
active: true,
},
});
console.log(res.data);
}
3. axios ์ธ์คํด์ค
๊ณตํต๋ ๊ธฐ๋ณธ URL์ด๋ ํค๋๋ฅผ ์ง์ ํด ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ธ์คํด์ค๋ฅผ ๋ง๋ค ์ ์๋ค.
// axios ์ธ์คํด์ค ์์ฑ
const api = axios.create({
baseURL: "API Base URL",
});
const todo = await api.get("/todos/1");
console.log(todo.data);
const user = await api.get("/users/1");
console.log(user.data);
4. ์๋ฌ์ฒ๋ฆฌ
fetch
๋ HTTP ์๋ฌ(400, 500)๊ฐ ๋ฐ์ํด๋ reject๋์ง ์๊ธฐ ๋๋ฌธ์
์๋ฌ ์ฒ๋ฆฌ๋ฅผ res.ok
์์ฑ์ ์ฌ์ฉํด์ ๋ณ๋๋ก ๊ตฌํํด์ผ ํ๋ค.
axios
๋ ์๋์ผ๋ก Promise rejected ์ํ๊ฐ ๋๊ธฐ ๋๋ฌธ์ ์๋ฌ์ฒ๋ฆฌ๊ฐ ํธ๋ฆฌํ๋ค.
// fetch
async function getNotFoundFetch() {
try {
const res = await fetch("/not-found");
if (!res.ok) {
throw new Error("HTTP Error: " + res.status);
}
const data = await res.json();
console.log(data);
} catch (err) {
console.error("Fetch Error:", err);
}
}
// axios
async function getNotFoundAxios() {
try {
const res = await axios.get("/not-found");
console.log(res.data);
} catch (err) {
console.error("Axios Error:", err.response.status); // ๋ฐ๋ก ์ํ ์ฝ๋ ํ์ธ ๊ฐ๋ฅ
}
}
๐ ์ฐธ๊ณ
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive
์์ํ๊ธฐ | Axios Docs
์์ํ๊ธฐ ๋ธ๋ผ์ฐ์ ์ node.js์์ ์ฌ์ฉํ ์ ์๋ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Axios๋? Axios๋ node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ์ ๋๋ค. ๊ทธ๊ฒ์ ๋ํ ์ ๋๋ค(๋์ผํ ์ฝ
axios-http.com