天天看點

react-native中post請求

在react-native中調用接口有3中方法,這會兒說一下fetch 的post請求示例

let formData = new FormData();
        formData.append("userName", this.state.userName);
        formData.append("password", this.state.password);
        formData.append("type", this.state.buttonType ? 1 : 2);
        let url = config.serverUrl + "/User/LoginOrRegister/login";
        let opts = {
            method: "POST",   //請求方法
            body: formData,   //請求體
        }
        fetch(url, opts)
            .then((response) => response.json())
            .then((responseData) => {
                // 注意,這裡使用了this關鍵字,為了保證this在調用時仍然指向目前元件,我們需要對其進行“綁定”操作
                if (responseData.data) {
                    alert(JSON.stringify(responseData.data))
                } else {
                    alert(responseData.message)
                }
            });
           

繼續閱讀