Axios
๐ Axios
Axios ๋
- Axios๋ Vue์์ ๊ถ๊ณ ํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
- Promise ๊ธฐ๋ฐ์ HTTP ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ฉฐ ์๋์ ์ผ๋ก ๋ค๋ฅธ HTTP ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๋นํด ๋ฌธ์ํ๊ฐ ์๋์ด ์๊ณ API๊ฐ ๋ค์ํ๋ค.
- axios.get(URL) -> Promise ๊ฐ์ฒด ๋ฆฌํด
- then ( ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ ๋ ), catch ( ์ค๋ฅ๊ฐ ๋ฐ์ํ ๋ ) ์ฌ์ฉ๊ฐ๋ฅ
- Promise
- ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ์ฌ ๋ฐ์์ค๋ ๋์๊ณผ ๊ฐ์ ๋น๋๊ธฐ ๋ก์ง ์ฒ๋ฆฌ์ ์ ์ฉํ ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์๋ฐ ์คํฌ๋ฆฝํธ๋ ๋จ์ผ ์ค๋ ๋๋ก ํน์ ๋ก์ง์ ์ฒ๋ฆฌ๊ฐ ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ฃผ์ง ์๋๋ค.
- ๋ฐ๋ผ์ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ณ ๋ฐ์์ฌ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ํ๋ฉด์ ๋ํ๋ด๋ ๋ก์ง์ ์คํํด์ผ ํ ๋ ์ฃผ๋ก promise๋ฅผ ํ์ฉ
Axios ์ค์น
- CDN ๋ฐฉ์
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
- NPM ๋ฐฉ์
- npm install axios
Axios ๋ํ API
-
axios.get(โURL ์ฃผ์โ).then().catch()
- ํด๋น URL ์ฃผ์์ ๋ํด HTTP GET ์์ฒญ์ ๋ณด๋
- ์๋ฒ์์ ๋ณด๋ธ ๋ฐ์ดํฐ๋ฅผ ์ ์์ ์ผ๋ก ๋ฐ์์ค๋ฉด then() ์์ ์ ์๋ ๋ก์ง์ด ์คํ๋๊ณ
- ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ฌ ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด catch()์ ์ ์ํ ๋ก์ง์ด ์คํ
-
axios.post(โURL ์ฃผ์โ).then().catch()
- ํด๋น URL ์ฃผ์์ ๋ํด HTTP POST ์์ฒญ์ ๋ณด๋
- then()๊ณผ catch() ๋ฐฉ์์ get ๋ฐฉ์๊ณผ ๋์ผ
-
axios({์ต์
์์ฑ})
- HTTP ์์ฒญ์ ๋ํ ์์ธํ ์์ฑ๋ค์ ์ง์ ์ ์ํด์ ๋ณด๋ผ ์ ์๋ค.
- ๋ฐ์ดํฐ ์์ฒญ์ ๋ณด๋ผ URL, HTTP ์์ฒญ๋ฐฉ์, ๋ณด๋ด๋ ๋ฐ์ดํฐ ์ ํ ๋ฑ๋ฑ
Axios ์์
axios(config)
json ์์ ํ์ผ ( jsonplaceholder )์ ์ด์ฉํ ์ค์ต
- GET
// jsonplaceholder
const URL = "https://jsonplaceholder.typicode.com/todos";
axios({
method: "get",
url: URL,
responseType: "json",
}).then((response) => {
console.log(response.data[1]);
});
- ๊ฒฐ๊ณผ
- POST
// jsonplaceholder
const URL = "https://jsonplaceholder.typicode.com/todos";
axios({
method: "post",
url: URL,
data: {
userId: "123",
id: "123",
title: "JS",
},
}).then((response) => {
console.log(response.data);
});
- ๊ฒฐ๊ณผ
axios.get(url[,config])
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ
// 1.
axios({
method: "get",
url: "/board/10",
});
// 2. axios() default ๋ get ์ด๋ค.
axios("/board/10");
// 3.
axios.get("/board/10");
- ํ์ฉ
// jsonplaceholder
const URL = "https://jsonplaceholder.typicode.com/todos";
axios
.get(URL)
.then((response) => {
console.log("1. 10๋ฒ์งธ ๋ฐ์ดํฐ ID : ", response.data[10].id);
return response.data[10].id;
})
.then((id) => {
console.log("2. 10๋ฒ์งธ ๋ฐ์ดํฐ ID : ", id);
return axios.get(`${URL}/${id}`);
})
.then((todo) => {
console.log("3. ์์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ์ TITLE : ", todo.data.title);
})
.catch((error) => {
console.log(error);
})
.finally(() => {
console.log("๋ !");
});
- ๊ฒฐ๊ณผ
axios.get(url[,config]) - async, await ์ฌ์ฉ
js์์ async/await๋ฅผ ์ด์ฉํ ๋น๋๊ธฐ ๊ตฌ๋ฌธ์ด ์ถ๊ฐ ๋์๊ธฐ์ axios๋ ์ด๋ฅผ ์ง์ํ๋ค.
๊ธฐ์กด then ๋ฐฉ๋ฒ๋ณด๋ค ๋ณด๊ธฐ์๋ ์์ฑํ๊ธฐ์๋ ๊น๋ํ๋ค.
// jsonplaceholder
const URL = "https://jsonplaceholder.typicode.com/todos";
async function getTodo(URL) {
const response = await axios.get(URL);
const id = response.data[10].id;
const todo = await axios.get(`${URL}/${id}`);
console.log(todo.data.id);
console.log(todo.data.title);
}
getTodo(URL);
- ๊ฒฐ๊ณผ
axios.post(url[,config])
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ
// 1.
axios({
method: "post",
url: "/board",
data: { userid: "js", subject: "์๋
ํ์ธ์" },
});
// 2.
axios.post("/board", {
userid: "js",
subject: "์๋
ํ์ธ์",
});
- ํ์ฉ
// jsonplaceholder
const URL = "https://jsonplaceholder.typicode.com/todos";
axios
.post(URL, {
userId: "js",
title: "hi hello",
})
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
})
.finally(() => {
console.log("๋!");
});
- ๊ฒฐ๊ณผ
axios.put(url[,config])
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ
// 1.
axios({
method: "put",
url: "/board/10",
data: { subject: "์๋
ํ์ธ์", content: "์ค๋๋ ์ข์ ํ๋ฃจ ๋์ธ์ ^^" },
});
// 2.
axios.put("/board/10", {
subject: "์๋
ํ์ธ์",
content: "์ค๋๋ ์ข์ ํ๋ฃจ ๋์ธ์ ^^",
});
axios.delete(url[,config])
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ
// 1.
axios({
method: "delete",
url: "/board/10",
});
// 2.
axios("/board/10", {
method: "delete",
});
// 3.
axios.delete("/board/10");