Axios๋?
- Axios๋ JavaScript๋ก ์์ฑ๋ ๊ฐ๋ ฅํ๊ณ ์ ์ฐํ HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์ฃผ๋ก ํด๋ผ์ด์ธํธ ์ฌ์ด๋์์ API ํธ์ถ์ ์ํํ๋ ๋ฐ ์ฌ์ฉํจ
- ๋ธ๋ผ์ฐ์ ์ Node.js ํ๊ฒฝ์์ ๋ชจ๋ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, HTTP ์์ฒญ์ ์ฝ๊ฒ ๋ง๋ค ์ ์๋๋ก ๋์์ค
- ๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฒ
- Axios๋ GET, POST, PUT, DELETE ๋ฑ ๋ค์ํ HTTP ๋ฉ์๋๋ฅผ ์ง์ํจ
// PUT ์์ฒญ
axios.put('/api/users/1', {
username: 'updateduser',
password: 'newpassword'
})
.then(response => {
console.log(response.data); // ์๋ฒ๋ก๋ถํฐ์ ์๋ต
})
.catch(error => {
console.error(error);
});
- Promise ๊ธฐ๋ฐ
- Axios๋ Promise ๊ธฐ๋ฐ์ผ๋ก ์๋
- ๋น๋๊ธฐ ์์ฒญ์ ์ฒ๋ฆฌํ ๋ .then()๊ณผ .catch()๋ฅผ ์ฌ์ฉํ์ฌ ์ฑ๊ณต์ ์ธ ์๋ต๊ณผ ์๋ฌ๋ฅผ ์ฝ๊ฒ ํธ๋ค๋งํ ์ ์์ + finally
axios.get('url')
.then(response => {
// ์ฑ๊ณต์ ์ธ ์๋ต ์ฒ๋ฆฌ
})
.catch(error => {
// ์ค๋ฅ ์ฒ๋ฆฌ
});
- ์์ฒญ๊ณผ ์๋ต ์ธํฐ์
ํธ
- ์์ฒญ ์ ์ ์์ฒญ์ ๊ฐ๋ก์ฑ๊ฑฐ๋, ์๋ฒ๋ก๋ถํฐ์ ์๋ต์ ๊ฐ๋ก์ฑ์ด ์ถ๊ฐ์ ์ธ ์์ ์ ํ ์ ์์
- ex) ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ ์ ํค๋์ ํ ํฐ์ ์ถ๊ฐํ๊ฑฐ๋, ์๋ต์ ๋ฐ์ ํ ํน์ ํฌ๋งท์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ๋ฑ์ ์์
axios.interceptors.request.use(request => {
console.log('Starting Request', request);
return request;
});
axios.interceptors.response.use(response => {
console.log('Response:', response);
return response;
});
- ์์ฒญ ์ทจ์
- ์งํ ์ค์ธ ์์ฒญ์ ์ทจ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต
- ex) ์ฌ์ฉ์๊ฐ ์ฌ๋ฌ ๋ฒ ํด๋ฆญํ์ฌ ์ค๋ณต ์์ฒญ์ ๋ณด๋ผ ๋ ์ ์ฉํจ
const CancelToken = axios.CancelToken;
let cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
cancel = c;
})
});
// ๋์ค์ ์์ฒญ์ ์ทจ์ํ๋ ค๋ฉด cancel ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
cancel();
- ๊ธ๋ก๋ฒ ์ค์
- Axios ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ค์ ์ ์ ์ํ ์ ์์
- ex) ๋ชจ๋ ์์ฒญ์ ๊ณตํต์ ์ผ๋ก ์ ์ฉ๋ ๊ธฐ๋ณธ URL, ํค๋, ํ์์์ ๋ฑ์ ์ค์ ํ ์ ์์
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.timeout = 2500;
- ์๋ฌ ํธ๋ค๋ง
- Axios ์์ฒญ์ด ์คํจํ ๋, ์๋ฌ ๊ฐ์ฒด๋ฅผ ํตํด ์ํ ์ฝ๋, ์๋ต ๋ฉ์์ง ๋ฑ์ ์ ๋ณด์ ์ ๊ทผํ ์ ์์
- ์ด๋ฅผ ํตํด ์ฌ์ฉ์์๊ฒ ์ ์ ํ ํผ๋๋ฐฑ์ ์ ๊ณตํ ์ ์์
axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
});
- ์๋ต ํ์
- Axios๋ JSON ํ์์ ์๋ต์ ์๋์ผ๋ก JavaScript ๊ฐ์ฒด๋ก ๋ณํํด์ค
- ๋ฐ์ดํฐ๋ฅผ ์ฆ์ ์ฌ์ฉํ ์ ์์์ ์๋ฏธํจ
axios.get('/user/12345')
.then(function (response) {
console.log(response.data); // ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ
console.log(response.status); // HTTP ์ํ ์ฝ๋
console.log(response.statusText); // ์ํ ๋ฉ์์ง
console.log(response.headers); // ์๋ต ํค๋
console.log(response.config); // ์์ฒญ ๊ตฌ์ฑ
});
- ํ์ผ ์
๋ก๋
- Axios๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์ ๋ก๋ํ ์ ์์
- FormData ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉํฐํํธ ํผ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์ ์กํ ์ ์์
const formData = new FormData();
formData.append('file', fileInput.files[0]);
axios.post('/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
- ๋ณด์ ์ฌํญ
- API ์์ฒญ์ ํ ๋ ๋ณด์์ ๊ณ ๋ คํด์ผํจ
- ์๋ฅผ ๋ค์ด, ํฌ๋ก์ค ์ฌ์ดํธ ์์ฒญ ์์กฐ(CSRF)์ ๊ฐ์ ๊ณต๊ฒฉ์ผ๋ก๋ถํฐ ๋ณดํธํ๊ธฐ ์ํด ์ ์ ํ ํ ํฐ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ค์
axios.get('/user/12345', {
withCredentials: true // ํฌ๋ก์ค ์ฌ์ดํธ ์์ฒญ ์ ์ธ์ฆ ์ ๋ณด(์ฟ ํค ๋ฑ)์ ํจ๊ป ๋ณด๋ด๊ธฐ ์ํ ์ค์
});
- Axios์ ๊ธฐ๋ฅ
- axios.create
- ์๋ก์ด Axios ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต
const apiClient = axios.create({
baseURL: 'https://api.example.com',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
- Axios์ ์ ์ญ ์ค์ ์ ์ ์ํ๋ ์์ฑ ๋๋ ํ๋กํผํฐ
- axios.default
- ์ ์ญ Axios ์ค์ ์ ์ ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต, Axios์ ์ ์ญ ์ค์ ์ ๋ด๊ณ ์๋ ๊ฐ์ฒด์ ํน์ ๊ฐ
axios.defaults.baseURL = 'https://api.example.com';
//axios.defaults๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ญ Axios ์ค์ ์ ๋ณ๊ฒฝํ ์ ์์
//์ด๊ฒ์ ๋ชจ๋ Axios ์์ฒญ์ ์ํฅ์ ๋ฏธ์นฉ๋๋ค
axios.get('/user', { timeout: 5000 });
//๊ฐ๋ณ Axios ์์ฒญ์ ์ง์ ๊ตฌ์ฑ์ ์ง์ ํ ์ ์์.ํด๋น ์์ฒญ์๋ง ์ ์ฉ๋จ
์ปจํธ๋กค๋ฌ ์์
@Controller
public class MyController {
@GetMapping("/get-data")
@ResponseBody
public ResponseEntity<String> getData() {
// ์คํ๋ง ์ปจํธ๋กค๋ฌ์์ ํด๋ผ์ด์ธํธ๋ก ์๋ตํ ๋ฐ์ดํฐ๋ฅผ ์์ฑ
String responseData = "Hello, this data is from the server!";
return ResponseEntity.ok(responseData);
}
}
JSP
<!DOCTYPE html>
<html>
<head>
<!-- Axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ CDN์ ํตํด ๊ฐ์ ธ์ต๋๋ค. -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<button id="getDataButton">๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ</button>
<div id="responseData"></div>
<script>
// ๋ฒํผ ํด๋ฆญ ์ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์์ ๊ฐ์ ธ์ค๋ ํจ์
document.getElementById('getDataButton').addEventListener('click', function () {
// Axios๋ฅผ ์ฌ์ฉํ์ฌ GET ์์ฒญ์ ์๋ฒ๋ก ๋ณด๋
๋๋ค.
axios.get('/get-data')
.then(function (response) {
// ์์ฒญ์ด ์ฑ๊ณตํ๋ฉด ์๋ต ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ํ์ํฉ๋๋ค.
document.getElementById('responseData').innerText = '์๋ฒ ์๋ต: ' + response.data;
})
.catch(function (error) {
// ์์ฒญ์ด ์คํจํ๋ฉด ์๋ฌ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
console.error('์๋ฌ:', error);
});
});
</script>
</body>
</html>
- ์์ ์์์์๋ Axios๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒํผ ํด๋ฆญ ์ ์๋ฒ๋ก GET ์์ฒญ์ ๋ณด๋ด๊ณ , ์๋ฒ์์ ์๋ตํ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ํ์ํ๋ ๊ฐ๋จํ ์์
- ์ด๋ ๊ฒ ์คํ๋ง๊ณผ JSP์์ ์๋ฐ์คํฌ๋ฆฝํธ์ Axios๋ฅผ ์ฌ์ฉํ์ฌ ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ์ ํต์ ์ ๊ตฌํํ ์ ์์
Thymeleaf
<!DOCTYPE html>
<html>
<head>
<!-- ํ์๋ฆฌํ๋ก ๋์ ์ธ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ๋ฑ์ ํ ์ ์์ต๋๋ค -->
<title>ํ์ด์ง ์ ๋ชฉ</title>
</head>
<body>
<h1>ํ์ด์ง ์ฝํ
์ธ </h1>
<!-- Axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํฌํจ -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
// Axios๋ฅผ ์ฌ์ฉํ API ํธ์ถ ์์
axios.get('/api/your-endpoint')
.then(function (response) {
// ์๋ต ์ฒ๋ฆฌ
console.log(response);
})
.catch(function (error) {
// ์๋ฌ ์ฒ๋ฆฌ
console.error(error);
});
</script>
</body>
</html>
'๐ฅ๏ธ ํ๋ก ํธ์๋ > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ณต์ ] ํฐ์คํ ๋ฆฌ ์๋ ๋ชฉ์ฐจ ๋ง๋ค๊ธฐ, ๋ฐ๋ผ๋ค๋๋ ์๋ ๋ชฉ์ฐจ ์์ฑ ๋ฐฉ๋ฒ, ๋ชฉ์ฐจ ๋ค๋น๊ฒ์ดํฐ ๋ง๋ค๊ธฐ (์์ ์ฌ์ ์ด๋ณด์ ๊ฐ๋ฅ) (1) | 2023.12.09 |
---|---|
[Script] ์ฒดํฌ๋ฐ์ค (1) | 2023.12.07 |
[Script] ํ์๊ฐ์ ํ์ด์ง ๊ธฐ๋ฅ JavaScript/JQuery๋ก ๊ตฌํํ๊ธฐ (1) | 2023.12.07 |
[JQuery] ์ ์ด์ฟผ๋ฆฌ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ (1) | 2023.12.05 |
[Node.js] Node.js ๋ง๋ณด๊ธฐ (0) | 2023.11.20 |