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

官方文档:
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}
     */
    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 (
            
                

源码下载
bkdemo2----HttpPost
https://download.csdn.net/download/zhaihaohao1/11022360

你可能感兴趣的:(ReactNative)