Axios

이동주·2022년 3월 11일

JavaScript

목록 보기
6/11

Axios란?

브라우저와 node.js에서 사용할 수 있는 Promise 기반 HTTP 클라이언트 라이브러리

Axios는 node.js와 브라우저를 위한 Promise 기반 HTTP 클라이언트 입니다. 서버 사이드에서는 네이티브 node.js의 http 모듈을 사용하고, 클라이언트(브라우저)에서는 XMLHttpRequests를 사용합니다.

쉽게 말해서 백엔드랑 프론트엔드랑 통신을 쉽게하기 위해 Ajax와 더불어 사용합니다.

특징

  • 운영 환경에 따라 브라우저의 XMLHttpRequest 객체 또는 Node.js의 http api 사용
  • Promise(ES6) API 사용
  • 요청과 응답 데이터의 변형
  • HTTP 요청 취소
  • HTTP 요청과 응답을 JSON 형태로 자동 변경
  • XSRF 보호
  • 대부분의 브라우저에서 지원이됨

GET / POST / DELETE / UPDATE 예시


// GET
axios.get('/user', { 
	params: { ID: 12345 } 
}) 
.then(function (response) { 
console.log(response); 
}) 
.catch(function (error) { 
console.log(error);
}) 
.finally(function () { 
// always executed 
});

// POST
axios.post("url", {
  firstName: 'Fred',
  lastName: 'Flintstone' 
}) 
 .then(function (response) {response })
 .catch(function (error) {
  // 오류발생시 실행 
})

// PUT
axios.put("url", {
  		username: "",
  	    password: "",
})
.then(function (response) {
	  // response
}).catch(function (error) {
	 // 오류발생시 실행
})

// DELETE
axios.delete("user?ID=12345")
	.then(function (response) {
  	  // hancle success
      console.log(response);
})
.catch(function (error) {
  // handle error
  console.log(error);
})


profile
생각나는대로 쓰는 개발 블로그

0개의 댓글