본문 바로가기

#IT 글로써

axios 사용법 - get, post, put

최근에 ajax 요청을 위해 가장 많이 사용되는 라이브러리는 axios이다.

 

이 axios를 사용해서 GET, POST, PUT, DELETE 요청을할 수 있는데 사용법은 매우 간단하다.

 

GET

import axios from 'axios';

axios.get(url).then((res) => {
    	// res.json();
    })
    .catch((err) => {
    });

 

POST

import axios from 'axios';

axios.post(url, data).then((res) => {
	return res.json();
    })
    .catch((err) => {
    });

위의 get, post 함수를 사용하는 대신 아래처럼 전체 configuration을 설정해서 사용하는 방법도 있다.

axios({
  method: 'post',  // get, post, put, delete 모두 가능
  url: {url},
  data: {}
});