Javascript
[Javascript] Axios 메서드 사용방법
먹세
2021. 8. 27. 16:37
Axios 는 Promise 기반의 브라우저, Node.js 를 위한 HTTP 요청 메소드 이다.
API 작업을 할 때 주로 사용한다.
이전에는 jQuery Ajax 를 많이 썼지만 요즘은 Axios를 많이 쓴다.
요즘은 jQuery를 아예 쓰지 않는 추세이기도 하고.
Asios를 사용하기 위해서는 우선 설치를 해야한다.
$ npm install axios
post 요청시 파라미터 전달 방법
axios.post('/url', {
id: myId,
name : myName
}).then(response => {
console.log(response.data);
}).catch(error => {
console.log(error.response);
});
get 요청시 파라미터 전달 방법
axios.get('/url', {
params: {
id: myId,
name : myName
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.log(error.response);
});
파라미터 작성문법이 약간 다르다.
get 에서는 params 를 포함하고, post 에서는 params를 포함하면 안된다.
반응형