์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- JS#3์ผ์ฐจ๋ฌ๋ฆฌ์#์ด๋ฐ์ธ๋ฐ#์๊ฐ๊ธ๋ฐฉ~
- CSS
- for~in/for~of
- ์๋ฐ์คํฌ๋ฆฝํธ#JS#slice#splice
- ๋ ธ๋๊ต๊ณผ์
- redux์ํ์ ์ง
- UI
- ์๋ฐ์คํฌ๋ฆฝํธ
- cmarket
- js
- Beesbeesbees
- @redux-toolkit
- ใทใ
- https://www.daleseo.com/js-array-slice-splice/
- variable#function
- https://developer-talk.tistory.com/299
- react
- toString#String
- UX
- removeCookie
- ํท๊ฐ๋ฆฐ๋ค~
- https://lo-victoria.com/introduction-to-redux-toolkit-for-beginners
- User Flow
- slice/splice/split
- ์๋ฐ์คํฌ๋ฆฝํธ#์กฐ๊ฑด๋ฌธ#๋ฌธ์์ด
- children vs childrenNodes
- dom
- ๋ด์ฅ๊ณ ์ฐจํจ์
- ์๋ฐ์คํฌ๋ฆฝํธ#JS#var#let#const#undefined#null
- https://dasima.xyz/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%A0%9C%EA%B3%B1-math-pow-%EA%B3%84%EC%82%B0/
- Today
- Total
Daily Front_Minhhk
[JS] Fetch API // Axios ๋ณธ๋ฌธ
๐ Axios๋ ๋ธ๋ผ์ฐ์ , Node.js๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
โ Axios๋ Fetch API๋ณด๋ค ์ฌ์ฉ์ด ๊ฐํธํ๋ฉด์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ๋ค์ด ํฌํจ๋์ด ์์ต๋๋ค.
Fetch API vs Axios
Axios | Fetch API |
์จ๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ค์น๊ฐ ํ์ํฉ๋๋ค. | ๋นํธ์ธ API๋ผ ๋ณ๋์ ์ค์น ํ์์์ต๋๋ค. |
์๋์ผ๋ก JSON๋ฐ์ดํฐ ํ์์ผ๋ก ๋ณํ๋ฉ๋๋ค. | .json() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. |
Axios๋ ์จ๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์น
โก๏ธ npm install axios
GET ์์ฒญ
GET ์์ฒญ์ ์ผ๋ฐ์ ์ผ๋ก ์ ๋ณด๋ฅผ ์์ฒญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋์ ๋๋ค.
์ฒซ๋ฒ์งธ ์ธ์์๋ url์ฃผ์๊ฐ ๋ค์ด๊ฐ๋๋ค.
url์ฃผ์๋ ํ์์ ๋๋ค.
๋๋ฒ์งธ ์ธ์์๋ ์์ฒญ ์ ์ฌ์ฉํ ์ ์๋ ์ต์ ๋ค์ ์ค์ ํ๊ฒ ๋ฉ๋๋ค.
์ต์ ์ ๊ฒฝ์ฐ ํ์๋ ์๋๋๋ค.
axios.get("url"[,config])
fetch API
Promise
→
// Promise ver
fetch('<https://koreanjson.com/users/1>', { method: 'GET' })
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
async_await
→
async function request() {
const response = await fetch('<https://koreanjson.com/users/1>', {
method: 'GET',
});
const data = await response.json();
console.log(data);
}
request();
.json()์ Response ์คํธ๋ฆผ์ ๊ฐ์ ธ์ ์คํธ๋ฆผ์ด ์๋ฃ๋ ๋๊น์ง ์ฝ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ค ์ฝ์ body์ ํ ์คํธ๋ฅผ Promiseํํ๋ก ๋ฐํํ๋ค.
axios
Promise
→
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Promise ver
axios
.get('<https://koreanjson.com/users/1>')
.then((response) => {
const { data } = response; // response ์ ๋ฐ์์์ data ํ ๋น
console.log(data); // object
})
.catch((error) => console.log(error));
async_await
→
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Async / Await ver
async function request() {
const response = await axios.get('<https://koreanjson.com/users/1>');
const { data } = response;
console.log(data);
}
request();
POST ์์ฒญ
POST ์์ฒญ์ ์๋ฒ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋์ ๋๋ค.
์ฒซ๋ฒ์งธ ์ธ์์๋ url์ฃผ์๊ฐ ๋ค์ด๊ฐ๋๋ค.
url์ฃผ์๋ ํ์์ ๋๋ค.
๋๋ฒ์งธ ์ธ์์๋ ์์ฒญ ์ ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๊ฒ ๋ฉ๋๋ค.
์ต์ ์ ๊ฒฝ์ฐ ํ์๋ ์๋์ง๋ง ์ํฉ์ ๋ฐ๋ผ ์ค์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
axios.post("url"[, data[, config]])
fetch API
Promise
→
// Promise ver
fetch('<https://koreanjson.com/users>', {
method: 'POST',
headers: {
// JSON์ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ค๋ค๊ณ ์๋ฒ์๊ฒ ์๋ ค์ฃผ๋ ์ญํ ์
๋๋ค.
'Content-Type': 'application/json',
},
body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
})
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
async_await
→
// Async / Await ver
async function request() {
const response = await fetch('<https://koreanjson.com/users>', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
});
const data = await response.json();
console.log(data);
}
request();
axios
Promise
→
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Promise ver
axios
.post('<https://koreanjson.com/users>', { nickName: 'ApeachIcetea', age: '20' })
.then((response) => {
// console.log(response)
const { data } = response; // ์ค๊ดํธ๋ก ๋ฐ์์ค์ ์
๋ ฅํ ๊ฐ์ฒด ์ ๋ณด data ๋ฅผ
console.log(data); // { nickName: 'ApeachIcetea', age: '20' }
})
.catch((error) => console.log(error));
async_await
→
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Async / Await ver
async function request() {
const response = await axios.post('<https://koreanjson.com/users>', {
name: 'ApeachIcetea',
age: '20',
});
const { data } = response;
console.log(data);
}
request();