먼저 AJAX에 대해 개념을 알고 가야하는 것 같아서, AJAX를 잠깐 정리하고 가겠다. 👇🏻
자바스크립트의 라이브러리 중 하나
서버와 통신하기 위해 XMLHttpRequest 객체를 사용
JSON, XML, HTML, 일반 텍스트 형식 등 다양한 포맷을 비동기로 주고받을 수 있음
전체 페이지를 새로 고치지 않고도, 페이지 일부만을 위한 데이터를 로드하는 기법
JavaScript를 사용한 비동기 통신, 클라이언트와 서버간에 XML 데이터를 주고받는 기술
자바스크립트를 통해서 서버에 데이터를 요청하는 것
axios 는 HTTP통신의 요구사항을 컴팩트한 패키지로써 사용하기 쉽게 설계 되었다.
( XML : Extensible Markup Language, 웹 사이트, 데이터 베이스 및 타사 애플리케이션과 같은 컴퓨터 시스템 간의 정보 교환을 지원 )
( 비동기 방식 : 웹페이지를 리로드하지 않고 데이터를 불러오는 방식 )
브라우저와 Node.js에서 사용할 수 있는 Promise 기반 HTTP 클라이언트 라이브러리.
( HTTP 비동기 통신 라이브러리 )
(* XMLHttpRequest(XHR) 객체는 페이지의 새로고침 없이도, URL에서 데이터를 가져올 수 있음.
이를 활용하여 사용자의 작업을 방해하지 않고 페이지의 일부를 업데이트할 수 있음!
AJAX 프로그래밍에서 많이 사용됨)
$ yarn add axios
$ npm i axios
// 프로젝트 상위에 import
import axios from 'axios'
axios({
//request
method: "get",
url: "url",
responseType: "type"
}).then(function (response) {
// response Action
...
});
(+) Axios 요청(request) 파라미터 옵션 중
withCredentials : cross-site access-control 요청을 허용 유무. 이를 true로 하면 cross-origin으로 쿠키값 전달 가능
Custom 속성을 지닌, 사용자 지정 config로 새로운 axios instance 를 만들 수 있음
Ex)
Const instance = axios.create({
baseURL: ‘https://domain/api/'
timeout: 1000,
header: {‘Custom-Header’ : ‘foobar’}
});
request, get, delete, head, options, post, put, patch, getUri
const url = 'http://localhost:3000/login'
const option = {
method:'POST',
header: {
'Content-Type':'application/json';charset=UTF-8'
},
body: JSON.stringify({
name: 'Ellie',
say: 'Hello'
})
fetch(url, options)
.then(res => console.log(res))
}
const option = {
url = 'http://localhost:3000/main'
method:'POST',
header: {
'Content-Type':'application/json';charset=UTF-8'
},
data:{
name: 'Ellie',
say: 'Boujour'
}
axios(options)
.then(res => console.log(res))
}
https://velog.io/@shin6403/React-axios%EB%9E%80-feat.-Fetch-API
https://axios-http.com/kr/docs/intro (axios 공식문서)