我正在尝试为我的本机注册屏幕返回一条错误消息。调用的.php脚本是:
<?php
include 'DBConfig.php';
// Creating connection.
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
if ($con == false) {
$ShitMSG = "Can't connect to database" ;
$ShitJson = json_encode($ShitMSG);
echo $ShitJson ;
}
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
// Populate User name from JSON $obj array and store into $name.
$name = $obj['name'];
// Populate User email from JSON $obj array and store into $email.
$email = $obj['email'];
//Checking Email is already exist or not using SQL query.
$CheckSQL = "SELECT * FROM UserRegistrationTable WHERE email='$email'";
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
if(isset($check)) {
$EmailExistMSG = "L'E-mail est déja utilisé !";
$EmailExistJson = json_encode($EmailExistMSG);
echo $EmailExistJson ;
}
else {
// Creating SQL query and insert the record into MySQL database table.
$Sql_Query = "insert into UserRegistrationTable (name,email) values ('$name','$email')";
if(mysqli_query($con,$Sql_Query)) {
$MSG = 'Utilisateur enregistré !' ;
$json = json_encode($MSG);
echo $json ;
}
else {
echo 'Réessayez';
}
}
mysqli_close($con);
?>
调用时,它会收到一个看起来像
的正文body: JSON.stringify({
name: UserName,
email: UserEmail
})
包含用户姓名和电子邮件的输入。 DBconfig.php是另一个PHP文件,我的数据库的ID是连接它的。
我的脚本由我的本机应用程序启动,来自此功能:
class LoginScreen extends React.Component {
constructor (props) {
super(props)
this.state = {
UserName: '',
UserEmail: ''
}
}
UserRegistrationFunction () {
const { UserName } = this.state
const { UserEmail } = this.state
fetch('http://c2isante.fr/appsante/login.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: UserName,
email: UserEmail
})
}).then((response) => response.json())
.then((responseJson) => {
Alert.alert(responseJson)
}).catch((error) => {
console.error(error)
})
}
render () {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 20, color: '#000', textAlign: 'center', marginBottom: 15 }}>User Registration Form</Text>
<TextInput
placeholder='Entrez votre nom'
onChangeText={UserName => this.setState({...this.state, UserName})}
underlineColorAndroid='transparent'
/>
<TextInput
placeholder='Entrez votre E-mail'
onChangeText={UserEmail => this.setState({...this.state, UserEmail})}
underlineColorAndroid='transparent'
/>
<Button title="M'enregistrer" onPress={this.UserRegistrationFunction.bind(this)} color='#2196F3' />
</View>
)
}
}
答案 0 :(得分:1)
您的PHP有三种可能的结果:
JSON编码的字符串:
$EmailExistMSG = "L'E-mail est déja utilisé !";
$EmailExistJson = json_encode($EmailExistMSG);
echo $EmailExistJson ;
JSON编码的字符串:
$MSG = 'Utilisateur enregistré !' ;
$json = json_encode($MSG);
echo $json ;
纯文本字符串:
echo 'Réessayez';
您应该保持一致:始终将输出编码为JSON或从不将输出编码为JSON。
或者:
除此之外:您没有指定header("Content-Type: something")
,因此无论您输出的是纯文本还是JSON,您都声称要输出HTML(PHP的默认值)。你应该解决这个问题。
答案 1 :(得分:0)
在你的php脚本中你直接json_encode
只返回一个字符串的字符串。
所以在你的javascript代码中从php文件接收字符串,你就可以在字符串上JSON.parse
。所以它给你错误。
答案 2 :(得分:0)
将字符串编码为json字符串有什么意义。直接回显字符串或创建响应数组并对其进行编码。
$x= array(
"error_code": "404",
"message": "not found"
);
echo json_encode($x);