๐ ฐ๏ธ AXIOS
๋ธ๋ผ์ฐ์ ์ node.js์์ ์ฌ์ฉํ ์ ์๋ Promise ๊ธฐ๋ฐ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ฝ๊ฒ ๋งํด, ๋ฐฑ์๋์ ํ๋ก ํธ์๋๊ฐ ํจ์จ์ ์ผ๋ก ํต์ ์ ํ๊ธฐ ์ํด Ajax์ ๋๋ถ์ด ์ฌ์ฉํ๋ ๊ฒ์ด๋ค
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
GET
axios.get(url,[,config])
get ์ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ฑฐ๋ ๋ณด์ฌ์ค ๋ ์ฌ์ฉํ๋ ์ฉ๋์ด๋ค.
์ฃผ์์ ๋ถ์ด์๋ ์ฟผ๋ฆฌ์คํธ๋ง์ผ๋ก ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค ๋ ์ด์ฉํ์ง ์์ ํ๊ฑฐ๋, ์ญ์ ํ๊ฑฐ๋ ๋ฑ์ ์ฉ๋๋ก๋ ์ฌ์ฉํ ์ ์๋ค๊ณ ํ๋ค.
// ์ง์ ๋ ID๋ฅผ ๊ฐ์ง ์ ์ ์ ๋ํ ์์ฒญ
axios.get('/user?ID=12345')
.then(function (response) { // ํ์ดํ ํจ์๋ ๊ฐ๋ฅ
// ์ฑ๊ณต ํธ๋ค๋ง
console.log(response);
})
.catch(function (error) {
// ์๋ฌ ํธ๋ค๋ง
console.log(error);
})
.then(function () {
// ํญ์ ์คํ๋๋ ์์ญ
});
// ์ ํ์ ์ผ๋ก ์์ ์์ฒญ์ ๋ค์๊ณผ ๊ฐ์ด ์ํ๋ ์ ์์ต๋๋ค.
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// ํญ์ ์คํ๋๋ ์์ญ
});
// async/await ์ฌ์ฉ์ ์ํ๋ค๋ฉด, ํจ์ ์ธ๋ถ์ `async` ํค์๋๋ฅผ ์ถ๊ฐํ์ธ์.
// ๋น๋๊ธฐ !!!!
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
POST
axios.post("url์ฃผ์",{
data๊ฐ์ฒด
},[,config])
post๋ ์๋ก์ด ๋ฆฌ์์ค๋ฅผ ์์ฑํ ๋ ์ฌ์ฉ๋๋ค.
post์ ๋ ๋ฒ์งธ ์ธ์(data๊ฐ์ฒด)๋ ๋ณธ๋ฌธ์ผ๋ก ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ ๋ฌ
๋ก๊ทธ์ธ, ํ์๊ฐ์ ๋ฑ ์ฌ์ฉ์๊ฐ ์์ฑํ ํ์ผ์ ์๋ฒ์๋ค๊ฐ ์ ๋ก๋ํ ๋ ์ฌ์ฉPost๋ฅผ ์ฌ์ฉํ๋ฉด ์ฟผ๋ฆฌ์คํธ๋ง์ด ๋จ์ง ์๊ธฐ๋๋ฌธ์ GET๋ณด๋ค ์์ ํ๋ค๊ณ ํจ!
axios.post('/user', { // ์ฆ ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ด {} ์์ ๋ฃ์ด์ฃผ๋ ๊ฒ
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
๐ฉ POST๋ฅผ ์ด์ฉํด์ ์ง๋ฌธ ์ ๋ก๋
const onSubmitQuestion = async (e) => {
e.preventDefault(); // ์ด๋ฒคํธ์ ๊ธฐ๋ณธ๊ธฐ๋ฅ์ ๋ง๊ณ ์ด ์ฝ๋ ๋จผ์ ์คํ
await axios
.post(
'http://์ด์ฉ๊ตฌ์ ์ฉ๊ตฌ๋ฐ์์จ์ฃผ์/questions',
{
memberId: Userdata.id,
title: title,
content: content,
},
{
headers: { authorization: localStorage.getItem('accessToken') }, // headers์ headers ๊ฐ์ฒด ์ ๋ฌ
}
)
.then(() => {
alert('์ง๋ฌธ ๋ฑ๋ก ์๋ฃ!')
//Swal.fire({ (๊ท์ฌ์ด ์๋ฆผ์ฐฝ)
// text: '์ง๋ฌธ ๋ฑ๋ก ์๋ฃ!',
// icon: 'success',
});
setTitle(null); // ์คํ ํ ์ ์๋ ๊ฒ ์์๋ณต๊ท
setContent(null);
navigate('/main'); // ๋ฉ์ธํ์ด์ง๋ก ์ด๋
})
.catch((e) => { // ์ค๋ฅ ์ ์ฝ์์ฐฝ์ ๋์
console.log(e);
});
};
return
...
<button onClick={onSubmitQuestion}> ์ง๋ฌธํ๊ธฐ </button>
DELETE
axios.delete(URL,[,config]);
REST ๊ธฐ๋ฐ API ํ๋ก๊ทธ๋จ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋์ด ์๋ ๋ด์ฉ์ ์ญ์ ํ๋ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
ใ
์๋ฒ์ ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ฉ์ ์ญ์ ํ๋ ๊ธฐ๋ฅ์ด๊ธฐ ๋๋ฌธ์ ๋๋ฒ์งธ ์ธ์๋ฅผ ์์ ์ ๋ฌํ์ง ์๋๋ค.
๐ delete๋ก ์ง๋ฌธ ์ญ์ ๊ตฌํ
const DeleteQuestion = async () => {
await axios
.delete(`http://~~์ฃผ์/questions/${pageData.id}`, {
id: pageData.id,
})
.then(() => {
navigate('/main', { replace: true }); // ์คํ ํ ๋ฉ์ธ์ผ๋ก ์ด๋
});
};
return ...
{pageData.memberId ===
Number(localStorage.getItem('UserId')) ? (
<QuDelete onClick={DeleteQuestion}>
Delete
</QuDelete>
) : (
''
)
}
PUT
axios.put(url[, data[, config]])
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋์ด ์๋ ๋ด์ฉ์ ๊ฐฑ์ ํ๋ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
axios.put('/user/12345', {
firstName: 'Fred',
lastName: 'Flintstone'
})
PATCH
axios.patch(url[, data[, config]])
๋ฐ์ดํฐ์ ์ ์ฒด๊ฐ ์๋ ์ผ๋ถ ๋ฐ์ดํฐ๋ง ์์ ํ๊ณ ์ถ๋ค๋ฉด patch๋ฅผ ์ด์ฉํ ์ ์๋ค.
patch๋ ๋๊ฒจ์ค ๋ฐ์ดํฐ๋ง ๋ณ๊ฒฝ๋๊ณ ๊ธฐ์กด ๋ฐ์ดํฐ๋ ์ ์ง๋๋ค.
โ๐ป patch๋ก ์ง๋ฌธ ์์ ๊ตฌํ
const modify = async (type, Id, title, content, answerId) => {
const result = await axios
.patch( // type์ด ์ง๋ฌธ, ๋ต๋ณ ๋ ์ค ํ๋
type == 'questions'
? `http://~~์ฃผ์:8080/${type}/${Id}`
: `http://~~์ฃผ์:8080/${type}/${answerId}`,
type == 'questions'
? {
Id,
title,
content,
}
: {
id: answerId,
questionId: Id,
content,
}
)
.then(() => {
alert('์์ ์๋ฃ!')});
init(questionId);
});
// ์๋ํ ์ด๊ธฐํํ์ฌ ํ๋ฉด ์
๋ฐ์ดํธ
};
return ...
{pageData.editState == false ? (
<QuEDContainer>
<QuEdit
onClick={
pageData.memberId === Number(localStorage.getItem('UserId'))
? OnclickEdit
: OnclickError
}
>
Edit
</QuEdit>
'TIL > etc.' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] Local Storage (0) | 2023.05.19 |
---|---|
[TIL] SweetAlert2 (0) | 2023.05.18 |
[TIL] ๋น๊ตฌ์กฐํ ํ ๋น (๊ตฌ์กฐ๋ถํดํ ๋น) (0) | 2023.05.18 |
[TIL] Emotion (0) | 2023.05.18 |
[TIL] JSX (1) | 2023.05.18 |