ReactNative学习----7Fetch之post请求数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaihaohao1/article/details/88566623

官方文档:
https://reactnative.cn/docs/network/#使用-fetch

例子,复制接口使用
HttpPost.js

/**
 *
 *
 * fetch 的post请求
 * https://reactnative.cn/docs/network/#使用-fetch
 *
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button} from 'react-native';


export default class HttpPost extends Component {
    /**
     * post请求数据
     * @returns {Promise<void>}
     */
    httpRequest = async () => {
        let url = `http://47.100.202.184:4000/login`;
        // 请求数据
        let rawData = await fetch(url,{
            method: 'POST',
            body: JSON.stringify({
                username: 'zhh1',
                password: '123456',
            }),
        });
        //拿到的数据转成json字符串
        let dataStr = await rawData.text();
        // 转化成json
        let jsonData = JSON.parse(dataStr);
        //打印结果
        console.warn(jsonData);
    }



    render() {
        return (
            <View style={styles.container}>
                <Button
                    onPress={() => this.httpRequest()}
                    title="post请求"
                />

            </View>
        );
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },

});

源码下载
bkdemo2----HttpPost

猜你喜欢

转载自blog.csdn.net/zhaihaohao1/article/details/88566623