2023. 8. 27. 18:44ใStudy_Develop
Ajax ๋?
์ฐ์ Ajax๋ Asynchronous JavaScript And XML์ ์ฝ์์ด๋ค.
Ajax์ ์ฝ์๋ฅผ ํ ๋๋ก ๋ณธ๋ ์๋ฏธ๋ฅผ ์ดํด๋ณด๋ฉด JavaScript๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํต์ , ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ธฐ์ ์ด๋ผ๊ณ ํ ์ ์๋ค.
html๊ฐ ์ฌํ ๊ธฐ์ ๋ค์ด ์ฌ์ฉํ๋ ์๋ก์ด ์ ๊ทผ๋ฒ์ด๋ผ๊ณ ์ค๋ช ํ๊ธฐ๋ ํ๋ค.
Ajax๋ฅผ ํตํด ์๋ฒ์ ๋น๋๊ธฐ์ ์ผ๋ก ํต์ ํจ์ผ๋ก ์ฐ๋ฆฌ๋ ์ ์ฒด ์น ํ์ด์ง๋ฅผ ๋ค์ ๋ถ๋ฌ์ค๋ ๋๊ธฐ ๋ฐฉ์๊ณผ๋ ๋ค๋ฅด๊ฒ ์ ์ง์ ์ผ๋ก ํด๋น ๋ถ๋ถ์ ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ๊ฐฑ์ ํ ์ ์๋ค.
๊ฐ๋จํ๊ฒ ๋งํด Ajax๋ js์์ ๋น๋๊ธฐ httpํต์ ์ด ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
๋น๋๊ธฐ httpํต์ ์ด๋ response์ request๋ฅผ ๋น๋๊ธฐ ์์ผ๋ก ๋ค๋ฃฐ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธ
์ฆ, ๋ฐ์ดํฐ๋ฅผ ์ด๋ํ๊ณ ํ๋ฉด์ ๊ตฌ์ฑํ๋๋ฐ ์์ด ํ๋ฉด ๊ฐฑ์ ์์ด ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ๋ณด๋ด๊ณ ์๋ต์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด๊ฑธ ์ ์ฌ์ฉํ๋ ๊ฑธ๊น?
- ๋จ์ํ๊ฒ ์๊ฐํ๋ฉด WEBํ๋ฉด์์ ๋ฌด์์ธ๊ฐ ๋ถ๋ฅด๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ์ถ์๋ ํ์ด์ง ์ ์ฒด๋ฅผ ์๋ก๊ณ ์นจํ๊ณ ์ถ์ง ์์์๋ผ๊ณ ํํํ ์ ์๋ค.
๋ฐ์ดํฐ์ ํ์์ผ๋ก๋ ํฌ๊ฒ, csv, json, xml ํ์์ด๋ค.
AJAX๋ก GET/ POST์์ฒญ ํ๋ ๋ฐฉ๋ฒ 3๊ฐ์ง
- XMLHttpRequest ๋ผ๋ ์๋ ๋ฌธ๋ฒ ์ฐ๊ธฐ
- fetch () ๋ผ๋ ์ต์ ๋ฌธ๋ฒ ์ฐ๊ธฐ
- axios๊ฐ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฐ๊ธฐ
Axios ๋?
- ์ด์ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ธ๋ผ์ฐ์ ์ xml http Request ๊ฐ์ฒด ๋๋ Node.js์ http api์ฌ์ฉ
- Promise(ES6) api ์ฌ์ฉ
- ์์ฒญ๊ณผ ์๋ต ๋ฐ์ดํฐ์ ๋ณํ
- http์์ฒญ ์ทจ์
- http ์์ฒญ๊ณผ ์๋ต์ jsonํํ๋ก ์๋ ๋ณ๊ฒฝ
npm install axios
์ผ๋จ axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์น๊ฐ ํ์ํ๋ค!
ํด๋ผ์ด์ธํธ(html) ์์ axios๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด, (jsDeliver/ unpkg CDN ๋ ์ค ํ)
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
์์ ์ฝ๋๋ฅผ ์ ์ด์ฃผ์ด์ผ ํ๋ค.
Axios๋ Promise based HTTP client for the browser and node.js
์ฆ, node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ HTTPํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๋น๋๊ธฐ๋ก HTTP ํต์ ์ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ฉฐ return์ promise ๊ฐ์ฒด๋ก ํด์ฃผ๊ธฐ ๋๋ฌธ์ response ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๊ธฐ๋ ์ฝ๋ค.
Axios ๋ฌธ๋ฒ ๊ตฌ์ฑ
axios ์์ฒญ(request) ํ๋ผ๋ฏธํฐ ์ต์
- method : ์์ฒญ ๋ฐฉ์ ( get ์ด ๋ํดํธ์.)
- url : ์๋ฒ ์ฃผ์
- baseURL : url์ ์๋๊ฒฝ๋ก๋ก ์ธ ๋ url ๋งจ ์์ ๋ถ๋ ์ฃผ์
- headers : ์์ฒญ ํค๋
- data: ์์ฒญ ๋ฐฉ์์ด put, post, patch ํด๋นํ๋ ๊ฒฝ์ฐ body์ ๋ณด๋ด๋ ๋ฐ์ดํฐ
- params : url ํ๋ผ๋ฏธํฐ (?key= value ๋ก ์์ฒญํ๋ url get ๋ฐฉ์์ ๊ฐ์ฒด๋ก ํํํ ๊ฒ)
- timeout : ์์ฒญ timeout์ด ๋ฐ๋ ๋๊ธฐ ์ milliseconds์ ์๊ฐ์ ์์ฒญ. timeout๋ณด๋ค ์์ฒญ์ด ๊ธธ์ด์ง๋ค๋ฉด, ์์ฒญ์ ์ทจ์๋๊ฒ ๋๋ค.
- responseType : ์๋ฒ๊ฐ ์๋ตํด์ฃผ๋ ๋ฐ์ดํฐ์ ํ์ ์ง์
- responseEncoding : ๋์ฝ๋ฉ ์๋ต์ ์ฌ์ฉํ๊ธฐ ์ํ ์ธ์ฝ๋ฉ (utf-8)
- transformRequest : ์๋ฒ์ ์ ๋ฌ๋๊ธฐ ์ ์ ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ฟ ์ ์๋ค.
- withCredentials : cross-site access-control ์์ฒญ์ ํ์ฉ ์ ๋ฌด, ์ด๋ฅผ true๋ก ํ๋ฉด cross-origin์ผ๋ก ์ฟ ํค ๊ฐ์ ์ ๋ฌํ ์ ์๋ค.
- auth : http ์ ๊ธฐ๋ณธ ์ธ์ฆ์ ์ฌ์ฉ. auth๋ฅผ ํตํด์ http์ ๊ธฐ๋ณธ ์ธ์ฆ์ด ๊ตฌ์ฑ ๊ฐ๋ฅ
- maxContextLength : http์๋ต ๋ด์ฉ์ max์ฌ์ด์ฆ๋ฅผ ์ง์ ํ๋๋ก ํ๋ ์ต์
- validataStatus : http์๋ต ์ํ ์ฝ๋์ ๋ํด promise์ ๋ฐํ ๊ฐ์ด resolve ๋๋ reject ํ ์ง ์ง์ ํ๋ ์ต์
- httpAgent/ httpsAgent : node.js์์ http ๋ https๋ฅผ ์์ฒญ์ ํ ๋ ์ฌ์ฉ์ ์ ์ agent๋ฅผ ์ ์ํ๋ ์ต์
- proxy : proxy ์๋ฒ์ hostname๊ณผ port๋ฅผ ์ ์ํ๋ ์ต์
- cancelToken : ์์ฒญ์ ์ทจ์ํ๋๋ฐ ์ฌ์ฉ๋์ด ์ง๋ ์ทจ์ ํ ํฐ์ ๋ช ์
axios ์๋ต (response) ๋ฐ์ดํฐ
axios๋ฅผ ํตํด ์์ฒญ์ ์๋ฒ์๊ฒ ๋ณด๋ด๋ฉด, ์๋ฒ์์ ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ํด๋ผ์ด์ธํธ์ ์๋ตํ๊ฒ ๋๋ค.
์ด๋ฅผ .then์ผ๋ก ํจ์์ธ์๋ก (response)๋ก ๋ฐ์ ๊ฐ์ฒด์ ๋ด๊ธด ๋ฐ์ดํฐ๊ฐ ๋ฐ๋ก ์๋ต ๋ฐ์ดํฐ์ด๋ค.
ajax๋ฅผ ํตํด ์๋ฒ๋ก๋ถํฐ ๋ฐ๋ ์๋ต์ ์ ๋ณด๋ ๋ค์๊ณผ ๊ฐ๋ค.
axios({
method: "get", // ํต์ ๋ฐฉ์
url: "www.naver.com", // ์๋ฒ
})
.then(function(response) {
console.log(response.data)
console.log(response.status)
console.log(response.statusText)
console.log(response.headers)
console.log(response.config)
})
response.data: {}, // ์๋ฒ๊ฐ ์ ๊ณตํ ์๋ต(๋ฐ์ดํฐ)
response.status: 200, // `status`๋ ์๋ฒ ์๋ต์ HTTP ์ํ ์ฝ๋
response.statusText: 'OK', // `statusText`๋ ์๋ฒ ์๋ต์ผ๋ก ๋ถํฐ์ HTTP ์ํ ๋ฉ์์ง
response.headers: {}, // `headers` ์๋ฒ๊ฐ ์๋ต ํ ํค๋๋ ๋ชจ๋ ํค๋ ์ด๋ฆ์ด ์๋ฌธ์๋ก ์ ๊ณต
response.config: {}, // `config`๋ ์์ฒญ์ ๋ํด `axios`์ ์ค์ ๋ ๊ตฌ์ฑ(config)
response.request: {}
// `request`๋ ์๋ต์ ์์ฑํ ์์ฒญ
// ๋ธ๋ผ์ฐ์ : XMLHttpRequest ์ธ์คํด์ค
// Node.js: ClientRequest ์ธ์คํด์ค(๋ฆฌ๋๋ ์
)
axios ๊ธฐ๋ณธ ํ์
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Ziihyun',
lastName: 'Lim'
}
});
-> ๊ธฐ๋ณธ ํ์
import axios from 'axios'
function App(){
return (
<button onClick={()=>{
axios.get('url์ฃผ์').then((๊ฒฐ๊ณผ)=>{
console.log(๊ฒฐ๊ณผ.data)
})
.catch(()=>{
console.log('์คํจํจ')
})
}}>๋ฒํผ</button>
)
}
1. axios๋ฅผ ์ฐ๋ ค๋ฉด ์๋จ์์ importํด์ค๊ณ
2. axios.get(URL) ์ด๋ฌ๋ฉด ๊ทธ URL๋ก GET์์ฒญ์ด ๋๋ค.
3. ๋ฐ์ดํฐ ๊ฐ์ ธ์จ ๊ฒฐ๊ณผ๋ ๊ฒฐ๊ณผ.data ์์ ๋ค์ด์๋ค.
๊ทธ๋์ ์์ ๋ฒํผ ๋๋ฅด๋ฉด ์๋ฒ์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๊ฐ ์ฝ์์ฐฝ์ ์ถ๋ ฅ๋๋ค.
4. ์ธํฐ๋ท์ด ์๋๊ฑฐ๋ URL์ด ์ด์ํ๋ฉด ์คํจํ๋๋ฐ
์คํจํ์ ๋ ์คํํ ์ฝ๋๋ .catch() ์์ ์ ์ผ๋ฉด ๋๋ค.
axios GET
get ๋ฉ์๋์๋ 2๊ฐ์ง ์ํฉ์ด ์กด์ฌํจ.
1. ๋จ์ ๋ฐ์ดํฐ (ํ์ด์ง ์์ฒญ, ์ง์ ๋ ์์ฒญ) ์์ฒญ์ ์ํํ ๊ฒฝ์ฐ
2. ํ๋ผ๋ฏธํฐ ๋ฐ์ดํฐ๋ฅผ ํฌํจ์ํค๋ ๊ฒฝ์ฐ (์ฌ์ฉ์ ๋ฒํธ์ ๋ฐ๋ฅธ ์กฐํ)
// user์๊ฒ ํ ๋น๋ id ๊ฐ๊ณผ ํจ๊ป ์์ฒญ์ ํฉ๋๋ค.
axios.get('/user?ID=12345')
.then(function (response) {
// ์ฑ๊ณตํ์ ๋
console.log(response);
})
.catch(function (error) {
// ์๋ฌ๊ฐ ๋ฌ์ ๋
console.log(error);
})
.finally(function () {
// ํญ์ ์คํ๋๋ ํจ์
});
axios POST
post ๋ฉ์๋์๋ ์ผ๋ฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ Message Body ์ ํฌํจ์์ผ ๋ณด๋ธ๋ค.
axios.post("url", {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
// response
}).catch(function (error) {
// ์ค๋ฅ๋ฐ์์ ์คํ
})
AJAX ์์ฒญ์ ๋์์ ์ฌ๋ฌ๊ฐ ๋ ๋ฆฌ๋ ค๋ฉด?
Promise.all([axios.get('URL1', axios.get('URL2')])
-> url1, url2 ๋ก get์์ฒญ์ ๋์์ ์งํํ๋ค.
๋ ๋ค ์๋ฃ์ ํน์ ์ฝ๋๋ฅผ ์คํํ๊ณ ์ถ์ผ๋ฉด .then()๋ค์ ๋ถ์ด๋ฉด ๋๋ค.
Promise.all()
Promise.all()์ ์ฌ๋ฌ๊ฐ์ ํ๋ก๋ฏธ์ค(Promise)๋ฅผ ๋น๋๊ธฐ์ ์ผ๋ก ์คํํ๋ค.
๋ฐ๋ผ์ ์ฌ๋ฌ ๊ฐ์ Promise์ฒ๋ฆฌ๋ฅผ ๋น๋๊ธฐ์ ์ผ๋ก ์คํํ๊ณ ์ ํ ๋, ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
async function sampleFunc(): Promise<void> {
// time start
console.time('promise all example');
// first promise
const fetchNameList = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result: any = ['Jack', 'Joe', 'Beck'];
resolve(result);
}, 300);
});
};
// second promise
const fetchFruits = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result: any = ['Apple', 'Orange', 'Banana'];
resolve(result);
}, 200);
});
};
// third promise
const fetchTechCompanies = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result: any = ['Apple', 'Google', 'Amazon'];
resolve(result);
}, 400);
});
};
// promise all
const result: any[] = await Promise.all([
fetchNameList(),
fetchFruits(),
fetchTechCompanies(),
]);
// time end
console.timeEnd('promise all example');
console.log('%j', result);
}
// execute
sampleFunc();
-> ํด๋น ์ฝ๋์ ์ ์ฒด ์คํ ์๊ฐ์ 402ms์ด๋ค. ๊ฐ Promise๋ค์ ์ฒ๋ฆฌ ์๊ฐ(300ms, 200ms, 400ms) ์ค ๊ฐ์ฅ ๊ธด ์๊ฐ์ธ 400ms์ ๋น์ทํ ์๊ฐ์ด๋ค. ์ด๋ฅผ ํตํด ๋น๋๊ธฐ ์ ์ผ๋ก ์คํ๋์์์ ์ ์ ์๋ค.
์๋ฌ ์ฒ๋ฆฌ
Promise.all() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋ ์ฃผ์ํด์ผ ํ ์ !
ํ๋ผ๋ฏธํฐ๋ก ์ฌ๋ฌ ๊ฐ์ Promise๋ค์ด ๋ฐฐ์ด ํํ๋ก ๋ค์ด๊ฐ๊ฒ ๋๋๋ฐ, ์ด ์ค ํ๋๋ผ๋ reject๊ฐ ๋๊ฑฐ๋ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ, ๋ชจ๋ Promise๋ค์ด reject ๋๋ค.
// third promise
const fetchTechCompanies = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result: any = ['Apple', 'Google', 'Amazon'];
reject(result);
}, 400);
});
};
Error: thrown: Array [
"Apple",
"Google",
"Amazon",
]
-> ์ถ๋ ฅ ๊ฒฐ๊ณผ
๋ฐ๋ผ์, Promise.all()์ ์ฌ์ฉํ ๋ ์๋ ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ํ ๊ฒ์ธ๊ฐ๋ฅผ ์ ํ๋ ๊ฒ์ด ์ค์ํ๋ค.
// result
let result: any[] = [];
// parameter array
const promiseList: any[] = [
fetchNameList(),
fetchFruits(),
fetchTechCompanies(),
];
// try ~ catch
try {
result = await Promise.all(promiseList);
} catch (e) {
// default value: empty array
result = Array(promiseList.length).fill([]);
console.error('promise-all', e);
}
-> ์ธ ๊ฐ์ง ๋ค๋ฅธ ๋น๋๊ธฐ ํจ์(fetchNameList, fetchFruits, fetchTechCompanies)๋ฅผ ๋ณ๋ ฌ๋ก ์คํํ๊ณ , ๋ชจ๋ Promise๊ฐ ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ ๋ค์ ๊ฒฐ๊ณผ๋ฅผ result ๋ฐฐ์ด์ ์ ์ฅํ๋ JavaScript ์ฝ๋์ด๋ค.
object / array ์๋ฃ ์ฃผ๊ณ ๋ฐ๋ ๋ฒ
object/ array ๋ฅผ ์ฃผ๊ณ ๋ฐ์ผ๋ ค๋ฉด ๋ฐ์ดํ๋ฅผ ์ณ์ ๋ณด๋ด๋ฉด ๋๋ค.
"{"name" : "kim"}"
์ด๋ฐ๊ฑธ json์ด๋ผ๊ณ ํ๋ค.
json์ ๋ฌธ์ ์ทจ๊ธ์ ๋ฐ๊ธฐ ๋๋ฌธ์ ์๋ฒ์ ์์ ๋กญ๊ฒ ์ฃผ๊ณ ๋ฐ๋๋ค.
์ค์ ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ฉด (๊ฒฐ๊ณผ.data) ๋ฐ์ดํ ์ณ์ง json์ด ๋์์ผ ํ๋๋ฐ,
axios๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ json์ object/ array ๋ณํ์์ ์ ์๋์ผ๋ก ๊ฑฐ์น๊ณ ์ถ๋ ฅ๋๊ธฐ ๋๋ฌธ์
object/ array๊ฐ ์ถ๋ ฅ ๋๋ค.
Fetch
fetch๋ ES6๋ถํฐ JavaScript์ ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋ค์ด์๋ค.
promise๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ก๊ธฐ์ Axios์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋๋ฐ ์ด๋ ต์ง ์์ผ๋ฉฐ, ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ๋ ์ฅ์ ์ผ๋ก ์ธํด ์๋นํ ํธ๋ฆฌํ์ฃ . ์ฝ๋ ๋ํ ๊ฐ๋จ๋ค.
//fetch
const url ='http://localhost3000/test`
const option ={
method:'POST',
header:{
'Accept':'application/json',
'Content-Type':'application/json';charset=UTP-8'
},
body:JSON.stringify({
name:'zihyun',
age:22
})
fetch(url,options)
.then(response => console.log(response))
-> fecth ์ post method ๊ตฌํ
์ฐธ๊ณ >
https://inpa.tistory.com/entry/AXIOS-%F0%9F%93%9A-%EC%84%A4%EC%B9%98-%EC%82%AC%EC%9A%A9
๐ AXIOS ์ค์น & ํน์ง & ๋ฌธ๋ฒ ๐ฏ ์ ๋ฆฌ
Axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ Axios๋ ๋ธ๋ผ์ฐ์ , Node.js๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์๋ค. ์ฝ๊ฒ ๋งํด์ ๋ฐฑ์๋๋ ํ๋ก ํธ์๋๋ ํต์ ์ ์ฝ๊ฒํ๊ธฐ ์ํด Ajax์ ๋๋ถ์ด ์ฌ์ฉํ๋ค. ์ด๋ฏธ
inpa.tistory.com
'Study_Develop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Capstone#4. Axios instance ์์ฑํ๊ธฐ / Axios interceptors (0) | 2024.11.12 |
---|---|
Capstone#2. Firebase ์ธ์ฆ, ๋ง์ถคํ API ํตํฉ (0) | 2024.11.09 |
Capstone#1. Firebase Authentication ์ฌ์ฉํด์ ๋๋ง์ ์ดํ ๋ง๋ค๊ธฐ (2) | 2024.11.08 |
[axios] Header, Content-Type, MIME, Authorization, (0) | 2023.08.28 |
REST API๋? (0) | 2023.08.27 |