axios
2020. 12. 13. 16:25ㆍJavaScript & TypeScript
axios (액시오스)
Promise based HTTP client for the browser and node.js
- jQuery 를 사용할 때 AJAX 라고 보면 됨
설치
npm install axios
GET 요청
axios 를 사용해 GET 요청 하는 방법
const axios = require('axios');
// 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 () {
// ...
});
POST 요청
axios를 사용해 POST 요청하는 방법
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
멀티 요청
여러 개의 요청을 동시 수행할 경우 axios.all() 메서드 사용
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now complete
}));
- 더 많은데 여기까지만 보고 나머지는 필요할때 사이트 추가적으로 봐야할듯
※ 출처
'JavaScript & TypeScript' 카테고리의 다른 글
Next export 시 Image Optimization 설정 문제 (3) | 2022.01.03 |
---|---|
TypeORM synchronize true 시 이전 table 까지 생성 문제 (2) | 2021.12.26 |
react-router-dom (0) | 2020.12.13 |
create-react-app :: NPM, NPX (0) | 2020.12.13 |
React - LifeCycle API (0) | 2020.11.24 |