티스토리 뷰

JavaScript & TypeScript

axios

kingsubin 2020. 12. 13. 16:25

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
  }));

- 더 많은데 여기까지만 보고 나머지는 필요할때 사이트 추가적으로 봐야할듯

 

 

 

 


※ 출처

이듬.run/axios/guide/

github.com/axios/axios