๐ท Fetch
โFetch๋?
์๋ฐ์คํฌ๋ฆฝํธ์์ ๋น๋๊ธฐ์ ์ผ๋ก HTTP์์ฒญ์ ์ํํ๊ธฐ ์ํ ์ต์ API๋ก
XHR ๋ณด๋ค ๋์ฑ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
Promise๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ฌ then ๋๋ async/await ์ฌ์ฉ ๊ฐ๋ฅํ๋ค
Response๋ฅผ ๋ ๋ฒ์ ๋๋ ๋ฐ์ผ๋ฉฐ , Json์ผ๋ก ๋ณํ์ ์ง์ ํด์ฃผ์ด์ผํ๋ค.
โ Fetch๋ฅผ ์ฌ์ฉํ๋ ์ด์
Fetch๋ ๋ธ๋ผ์ฐ์ ๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ API ์ด๊ธฐ ๋๋ฌธ์ ๊ฐํธํ๊ฒ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ
๊ฐ๋จํ ๋คํธ์ํฌ ์์ฒญ์ ๊ฒฝ์ฐ ๊ฐ๋ฒผ์ด ์ฝ๋๋ก ํ์ฉ์ด ๊ฐ๋ฅํ๋ค.
โ๏ธ Fetch ๊ธฐ๋ณธ ๊ตฌ์กฐ
fetch("์์ฒญ ๋ณด๋ผ ์๋ฒ์ url")
.then((response1) => {
//์ฒซ๋ฒ์งธ response ์ฒ๋ฆฌ
}).then(response2) => {
//๋๋ฒ์งธ response ์ฒ๋ฆฌ
}).catch((error)=>{
//์คํจ ์ catch ์คํ
})
๐ fetch๋ response๋ฅผ ๋ ๋ฒ์ ๋๋ ์ ๋ฐ์ผ๋ฉฐ
์ฒซ๋ฒ์งธ์์๋ ์๋ต์ ๋ฐ๊ณ ๋๋ฒ์งธ์์๋ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅ
์คํจ ์ catch๋ฌธ ์คํ
๐ท Fetch ํ์ฉํ๊ธฐ
1๏ธโฃ ํจ์ ์คํ ๋ฒํผ ์์ฑ
<body> <h1>ํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ ์ ๋๋ค.</h1> <button onclick="test()">start</button> <div id="test"></div> </body>
๐ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ div ํ๊ทธ๊น์ง ์์ฑ
2๏ธโฃ fetch ์์ฑ
1. ์๋ต์ ๋ฐ์ then ์์ฑ
response.ok : ์๋ต ์ฑ๊ณต ์ฌ๋ถ๋ฅผ ๋ฐํfunction test() { fetch("test.html") .then((response) => { if (response.ok) return response.text(); else throw new Error("์คํจ"); }) }
๐ ์๋ต์ ์ฑ๊ณตํ ๊ฒฝ์ฐ ํด๋น ์คํ ๋ฌธ์์ text๋ฅผ ๋ฐํํ๊ณ ์คํจํ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋ฐ์์์ผ catch๋ก ์ ๋ฌ
2. ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ then ์์ฑfunction test() { fetch("ex01_Error.html") .then((response) => { if (response.ok) return response.text(); else throw new Error("์คํจ"); }) .then((response) => { document.getElementById("test").innerHTML = response; }) }
๐ ์ ๋ฌ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ ์์น์ ์ถ๋ ฅ
3. ์คํจํ ๊ฒฝ์ฐ ์ฒ๋ฆฌ๋ฅผ ์ํ catch ์์ฑfunction test() { fetch("ex01_Error.html") .then((response) => { if (response.ok) return response.text(); else throw new Error("์คํจ"); }) .then((responseJson) => { document.getElementById("test").innerHTML = responseJson; }) .catch((error) => { console.log(error); }); }
๐ ์คํจ ์ ์๋ฌ ๋ฉ์์ง ์ฝ์ ์ถ๋ ฅ
โ๏ธ ๊ฒฐ๊ณผ
์ฑ๊ณต
---------------------------------
์คํจ
๐ท Axios
โAxios๋?
์๋ฐ์คํฌ๋ฆฝํธ์์ ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌ ํ๊ธฐ์ํ HTTP ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก
Promise๊ณผ XHR์ ๊ธฐ๋ฐ์ผ๋ก ํ์ฌ ๋ ๋ฐฉ์์ ์ฅ์ ์ ๋ชจ๋ ๊ฐ์ง๊ณ ์๋ค.
Response๋ฅผ ํ ๋ฒ์ ๋ฐ์์ค๋ฉฐ ์์ฒญ๊ณผ ์๋ต์ Jsonํํ๋ก ์๋์ผ๋ก ์ฒ๋ฆฌํด์ค๋ค.
โ Axios๋ฅผ ์ฌ์ฉํ๋ ์ด์
Axios๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ๋ณ๋๋ก ์ค์นํด์ผ๋๋ค๋ ๋จ์ ์ด ์์ง๋ง
Fetch๋ณด๋ค ์ฌ์ฉ์ด ํจ์ฌ ํธ๋ฆฌํ๊ณ ๊ธฐ๋ฅ์ด ๋ง๋ค๋ ์ฅ์ ์ด์์ผ๋ฉฐ Json์ผ๋ก ์๋ ๋ณํ๋๋ค๋ ์ด์ ์ด ์๋ค.
๊ทธ๋ฆฌ๊ณ ์์ฒญ ์ทจ์๋ ํ์์์๋ฑ ์ฌ๋ฌ ๊ธฐ๋ฅ์ด ํ์ํ ๋ ์ฌ์ฉํ๋ฉด ์ข๋ค.
โ๏ธ Axios ๊ธฐ๋ณธ ๊ตฌ์กฐ
axios
//(get/post/put/delete)
.์์ฒญ ๋ณด๋ด๋ ๋ฐฉ์("์์ฒญํ ๋ฌธ์")
.then((response)=>{
//JSON ํํ๋ก ์๋ ๋ณํ๋์ด ๋ฐํ
})
.catch((error)=>{
//๋ฌธ์ ๋ฐ์ ์ ์๋์ผ๋ก catch ์คํ
})
๐ท Axios ํ์ฉํ๊ธฐ
1๏ธโฃ ํจ์ ์คํ ๋ฒํผ ์์ฑ
<body> <h1>ํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ ์ ๋๋ค.</h1> <button onclick="test()">start</button> <div id="test"></div> </body>
๐ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ div ํ๊ทธ๊น์ง ์์ฑ
2๏ธโฃ fetch ์์ฑ
1. ์ ์ก ๋ฐฉ์๊ณผ url ์ ์ ๋ฌํ๊ณ then์ผ๋ก ๋ฐ๊ธฐ
axios .get("test.html") .then((response) => { document.getElementById("test") .innerHTML = `** ์๋ฒ Response ์ฑ๊ณต axios =><br> ${response.data}`; }) } //axTest
๐ ์๋ต์ ์๋ JSON ๋ณํ๋์ด ๋ฐํ
2. ์๋ฌ ๋ฐ์ ์ ์คํํ catch๋ฌธaxios .get("test.html") .then((response) => { document.getElementById("test") .innerHTML = `** ์๋ฒ Response ์ฑ๊ณต axios =><br> ${response.data}`; }).catch(({ response, message }) => { //=> ๊ฐ์ฒด ๊ตฌ์กฐ๋ถํด ์ ์ฉ // err.response , err.message ๋ฑ document.getElementById( "resultArea" ).innerHTML = `** ์๋ฒ Response ์คํจ axios => <br> err.response = ${response} <br> err.response.status = ${response.status} <br> err.response.headers = ${response.headers} <br> err.message = ${message} `; }); } //axTest
๐ response๋ฅผ ๊ตฌ์กฐ๋ถํดํ์ฌ ์๋ฌ ๋ฉ์์ง ์ถ๋ ฅ