본문 바로가기
└ Vue.js

[Vue.js] Axios 사용

by 짜장이누나 2019. 1. 15.


Axios (액시오스)


Vue(뷰)에서 권고하며 가장 성공한 HTTP 클라이언트 라이브러리로 알려져있다.






Axios 사용법


1. GET 요청


기본구조

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 1.
axios.get('/url?id=elena90')
    .then( response => { console.log(response); } ) // SUCCESS
    .catch( error => { console.log(error); } ); // ERROR
 
// 2.
axios.get('/url', {
        params: { id: 'elena90' }
    })
    .then( response => { console.log(response) } );
    .catch( error => { console.log(error) } );
 
// catch 는 생략 될 수 있습니다.
 
 
cs


예제

1
2
3
4
5
6
7
8
9
10
11
axios.get('/URL', { crossDomain : true })
    .then(function(response) {
        if (response.status == 200) {
            // SUCCESS
            
        }
    })
    .catch(function (error) {
        // FAIL
    });
 
cs





2. POST, PUT 요청


기본구조

1
2
3
4
5
6
7
axios.post('/url', {
        user: 'elena90',
        message: 'axios example'
    })
    .then( response => { console.log(response) } )
    .catch( response => { console.log(response) } );
 
cs


예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// POST
axios.post('/posturl', JSON.stringify(jsonData),
    { headers: { 'Content-Type''application/json' } })
    .then(function(response) {
        if (response.status == 200) {
            console.log(response);
        }
    });
 
 
// PUT
axios.put('/puturl',
    { headers: { 'Content-Type''application/json' } })
    .then(function(response) {
        if (response.status == 200) {
            window.location.reload();
        }
    });
 
cs








(delete나 더 예제 생기면 업데이트 할 예정)