我正在尝试将数据插入到我的MySQL数据库中,但我在模拟器上收到一条错误消息“网络请求失败”。我猜fetch()
它与我的系统IP地址有关(即使我输入了正确的IP地址)。
App.js
文件是我的React Native代码。 DBConfig.php
文件标识我的数据库信息。 submit_user_info.php
文件负责填充数据库和config.inc.php
(此帖子中未包括,因为它太长)文件具有我的数据库配置。
注意:在MAMP中找到config.inc.php
> bin> phpMyAdmin> config.inc.php
注意:在DBConfig.php
中,我将$HostUser
和$HostPass
留空(='')
,因为在config.inc.php
内,在第568-569行,{{1} }和$cfg['SQLValidator']['username']
留空。
这是$cfg['SQLValidator']['password']
:
App.js
这是import React, { Component } from 'react';
import { AppRegistry, StyleSheet, TextInput, View, Alert, Button }
from 'react-native';
class ThisApp extends Component {
constructor(props) {
super(props)
this.state = {
TextInputName: '',
TextInputEmail: '',
TextInputPhoneNumber: ''
}
}
InsertDataToServer = () =>{
const { TextInputName } = this.state ;
const { TextInputEmail } = this.state ;
const { TextInputPhoneNumber } = this.state ;
fetch('https://MySystemsIPAddress/submit_user_info.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: TextInputName,
email: TextInputEmail,
phone_number: TextInputPhoneNumber
})
}).then((response) => response.json())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
}).catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.MainContainer}>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter Name"
onChangeText={TextInputName => this.setState({TextInputName})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter Email"
onChangeText={TextInputEmail => this.setState({TextInputEmail})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter Phone Number"
onChangeText={TextInputPhoneNumber => this.setState({TextInputPhoneNumber})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<Button title="Insert Text Input Data to Server" onPress={this.InsertDataToServer} color="#2196F3" />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: 'center',
flex:1,
margin: 10
},
TextInputStyleClass: {
textAlign: 'center',
marginBottom: 7,
height: 40,
borderWidth: 1,
// Set border Hex Color Code Here.
borderColor: '#FF5722',
// Set border Radius.
//borderRadius: 10 ,
}
});
export default ThisApp;
DBConfig.php
这是<?php
//Define your host here.
$HostName = "localhost";
//Define your database name here.
$DatabaseName = "UserInfo";
//Define your database username here.
$HostUser = "";
//Define your database password here.
$HostPass = "";
?>
:
submit_user_info.php