|RN||react-native| rest api 통신하기 :: ThinkTank

ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • |RN||react-native| rest api 통신하기
    프로그래밍/react-native 2021. 3. 4. 07:32

    앱개발중 로그인로직 구현위해 서버와 통신을 해야했다.

    import * as React from "react"
    export function postData(url = '', data = FormData) {
        return fetch(url, {
            method: 'POST',
            mode: 'cors',
            cache: 'no-cache',
            credentials: 'same-origin',
            headers: {
                'Content-Type': 'application/json',
            },
            redirect: 'follow', 
            referrer: 'no-referrer', 
            body: data, 
        })
        .then(response => response.json());
    }

    리액트 네이티브에서 기본적으로 제공하는 컴포넌트이며.

    간편하게 작업할수 있고 결과값 콜백처리하기도 용의하다.

     

    const data = new FormData();
    data.append('method','login');
    data.append('mb_id','test');
    data.append('mb_password','test');
    RestApi.postData(GLOBALS.URLS.TEST_URL, data)
    .then(data => 
    console.log(JSON.stringify(data)
    ))
    .catch(error => console.error(error));

    parameter 를 FormData 를 통해 세팅해서 전송하였으며.

     

    결과값 / 에러메시지를 log찍게 처리해놨다.

    추후에 그부분은 parse 로직을짜서 사용하기알맞게 조리해서 쓸 예정이다.

     

    '프로그래밍 > react-native' 카테고리의 다른 글

    |RN||react-native| svg 이미지 사용하기  (0) 2021.03.03

    댓글

Designed by Tistory.