react native POST data to the server returns the data acquisition routine or

react native end reference code:

Import React, {from} the Component 'REACT';
Import {
    AppRegistry,
    the StyleSheet,
    the Text,
    TouchableHighlight,
    the Alert,
    TouchableOpacity,
    View
} from 'Native-REACT';


// Post method, the body needs to request body
/ *
* FromData
* mainly used to serialize the same form and format of the data to create the form
*
* data var = the FormData new new ();
* data.append ( "name", "Hello");
* the append method receives two parameters, keys and values (key, value), respectively, represent the value of the name and the field form fields may be added a plurality
*
* in jQuery, "key1 = value1 & key2 = valu2" as a parameter object frame will be automatically distributed into FormData form
* in the Fetch, a post for post request,Automatically creates FormData object to the body
*
* * /
function postRequest (URL) {
    //将"key1=value1&key2=valu2" 形式封装整FromData形式
    let formData = new FormData();
    formData.append("id","15");
    formData.append("verName","1111aaaa");

    var opts = {
        method:"POST",   //请求方法
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body:formData,   //请求体
    };


   fetch(url , {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: formData,
    }).then((response) => {
        if (response.ok) {
            return response.json();
        }
    }).then((json) => {
        console.log(json);
        console.log(json.id);
       console.log(json.verName);
        alert(JSON.stringify(json));
    }).catch((error) => {
        console.error(error);
    })
}


export default class HomeScreen extends Component {
    render() {
        return(
            <View style={styles.container}>
                {/*注意: 方法调用方式,绑定了this */}
                <TouchableOpacity onPress={postRequest.bind(this,"http://jmbsjk.com/test/test2.php")}>
                    <View style={styles.btn}>
                        <Text>Post</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

var styles = StyleSheet.create({
    container:{
        flex:1,
        backgroundColor:'cyan',
        marginTop:30,
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center'
    },
    btn:{
        width:60,
        height:30,
        borderWidth:1,
        borderColor:"yellow",
        justifyContent:'center',
        alignItems:'center'
    }
});

 

PHP server-side code:

<?php

   // $json = file_get_contents("php://input");
   // $data = json_decode($json, true);
    //$id=$data['id'];
    
    $id=$_POST['id'];
    $verName=$_POST['verName'];
    
    //链接数据库
    $con = mysqli_connect("localhost","my_db","123456",'my_db',3306);

    mysqli_query the Result = $ (. $ CON, "the SELECT * from recordClear the WHERE recordId =" $ the above mentioned id ".");
    $ NUM = mysqli_num_rows ($ the Result);
    // printf (. "total return data line% d", $ num );
      // release the result set
    // mysqli_free_result ($ the result);
    // Gets week
    $ TIME2 = DATE ( "N", Time ());
    // printf ( "today:% S", $ TIME2);
    $ SQL = "the INSERT the INTO recordClear (recordld, recordCount, cleardate) the VALUES (" $ ID ", '. 1'," $ TIME2.... ")";
    $ Query = the mysqli_query ($ CON, $ SQL);
    mysqli_close ($ CON );

    $resulttt = array(
    'id'=>$id,
    'verName'=>$verName,            
    );
    echo json_encode($resulttt,128);

?>

 

Published 38 original articles · won praise 10 · Views 100,000 +

Guess you like

Origin blog.csdn.net/winux123/article/details/89447383