리액트

axios 와 fetch

훈츠 2020. 7. 10. 11:54
반응형

Axios 

 1. react 에 권고 

 2. 설치 필요 

 3. fetch 보다 기능 많음. 

 4. Promise based 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let url = 'https://someurl.com';
let options = {
            method: 'POST',
            url: url,
            headers: {
                'Accept''application/json',
                'Content-Type''application/json;charset=UTF-8'
            },
            data: {
                property_one: value_one,
                property_two: value_two
            }
        };
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
    let data = await response.data;
    // do something with data
}
 
cs

axios 는 status 가 200 인지와 statusText 값을 통해서 확인 합니다. 

Fetch

 

 1. react native 에 권고 

 2. 기본 내장

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let url = 'https://someurl.com';
let options = {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Accept''application/json',
                'Content-Type''application/json;charset=UTF-8'
            },
            body: JSON.stringify({
                property_one: value_one,
                property_two: value_two
            })
        };
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
    let data = await response.json();
    // do something with data
}
 
 
cs

fetch는 response.ok 를 통해서 확인 합니다. 

반응형