-
[JS] chapter22. ๋น๋๊ธฐ4 - AxioscodeStates front-end/Java Script 2023. 1. 19. 17:51๋ฐ์ํ
๐ ๋น๋๊ธฐ
๋น๋๊ธฐ ์ฒ๋ฆฌ๋, ํน์ ์ฝ๋์ ์คํ์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค์ ์ฝ๋๋ฅผ ์ํํ๋ ๊ฒ
js๋ ์ฑ๊ธ ์ค๋ ๋ ๊ธฐ๋ฐ ๋์ ์ธ์ด๋ผ์ ๋๊ธฐ์ ์๋์ด์ง๋ง ํ๊ฒฝ(๋ฐํ์)์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ๋์์ฃผ๊ธฐ ๋๋ฌธ์
๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ํ ๊ฐ๋ฅํ๋ค.
๐ป Axios
fetch API์ ๋น์ทํ ์ญํ ์ ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Axios๋ ๋ธ๋ผ์ฐ์ , Node.js๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Fetch API vs Axios
Axios Fetch API ์จ๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ค์น๊ฐ ํ์ํฉ๋๋ค. ๋นํธ์ธ API๋ผ ๋ณ๋์ ์ค์น ํ์์์ต๋๋ค. ์๋์ผ๋ก JSON๋ฐ์ดํฐ ํ์์ผ๋ก ๋ณํ๋ฉ๋๋ค. .json() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. Axios ์ฌ์ฉ๋ฒ
npm install axios // axios ์ค์น
GET ์์ฒญ
์ ๋ณด๋ฅผ ์์ฒญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
์ฒซ๋ฒ์งธ ์ธ์ : URL , ๋๋ฒ์งธ ์ธ์ : ์์ฒญ์ ์ฌ์ฉํ ์ ์๋ ์ต์ ๋ค
axios.get("url"[,config])
๐ fetch API์ axios GET ์์ฒญ์ ์ฌ์ฉ๋ฒ๊ณผ ์ฐจ์ด์
fetch API
fetch('https://koreanjson.com/users/1', { method: 'GET' }) .then((response) => response.json()) .then((json) => console.log(json)) .catch((error) => console.log(error)); // Async / Await ver // async function request() { // const response = await fetch('https://koreanjson.com/users/1', { // method: 'GET', // }); // const data = await response.json(); // console.log(data); // } // request(); const appDiv = document.getElementById('app'); appDiv.innerHTML = ` <h1>Fetch API ๐</h1> <h3>console ์ฐฝ์ ํ์ธํด์ฃผ์ธ์! ๐๐๐</h3> `;
axios GET
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค. import axios from 'axios'; // Promise ver axios .get('https://koreanjson.com/users/1') .then((response) => { const { data } = response; console.log(data); }) .catch((error) => console.log(error)); // Async / Await ver // async function request() { // const response = await axios.get('https://koreanjson.com/users/1'); // const { data } = response; // console.log(data); // } // request(); const appDiv = document.getElementById('app'); appDiv.innerHTML = ` <h1>Axios โบ๏ธ</h1> <h3>console ์ฐฝ์ ํ์ธํด์ฃผ์ธ์! ๐๐๐</h3> `;
์ฝ์ ๊ฒฐ๊ณผ (๋ ๊ฐ ๋์ผ)
POST ์์ฒญ
์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
์ฒซ๋ฒ์งธ ์ธ์ : URL , ๋๋ฒ์งธ ์ธ์ : ์์ฒญ ์ ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ค์
axios.post("url"[, data[, config]])
๐ fetch API์ axios POST ์์ฒญ์ ์ฌ์ฉ๋ฒ๊ณผ ์ฐจ์ด์
Fetch API์ POST์์ฒญ
// 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 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(); const appDiv = document.getElementById('app'); appDiv.innerHTML = ` <h1>Fetch API ๐</h1> <h3>console ์ฐฝ์ ํ์ธํด์ฃผ์ธ์! ๐๐๐</h3> `;
Axios์ POST์์ฒญ
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค. import axios from 'axios'; // Promise ver axios .post('https://koreanjson.com/users', { nickName: 'ApeachIcetea', age: '20' }) .then((response) => { const { data } = response; console.log(data); }) .catch((error) => console.log(error)); // 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(); const appDiv = document.getElementById('app'); appDiv.innerHTML = ` <h1>Axios โบ๏ธ</h1> <h3>console ์ฐฝ์ ํ์ธํด์ฃผ์ธ์! ๐๐๐</h3> `;
๊ฒฐ๊ณผ๋ VSCODE๋ก ํ์ธํด๋ณด์!
AXOIS๋ ์ ๋ง ์ค์! ๋ฐฑ์๋ ํ์ ์ ์ฃผ๊ตฌ์ฅ์ฐฝ ํ ๊ฑฐ๋ค ๊ผญ ์ธ์๋์ฅ
๋ฐ์ํ'codeStates front-end > Java Script' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] chapter23. Tree UI (0) 2023.02.14 [JS] ์ค์ต - fetch API (0) 2023.01.19 [JS] chapter21. ๋น๋๊ธฐ3 - fetch API (0) 2023.01.19 [JS] chapter20. ๋น๋๊ธฐ2 - node.js (0) 2023.01.19 [JS] ์ค์ต - ํ์ด๋จธ API (0) 2023.01.18