当我尝试比较if (responseJson === "wrong")
时,它总是转到else
,即使我发出警告,告诉我responseJson的值是“错误的”。
以下是PHP文件和Form.js的代码
<?php
include("Config.php");
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$username = $obj['username'];
$password = $obj['password'];
$link = mysqli_connect("$HostName", "$HostUser", "$HostPass", "$DatabaseName");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$check = mysqli_query($link,"SELECT mail FROM Usuarios WHERE `mail`= '$username' AND `contrasena`= '$password'");
$numrows = mysqli_num_rows($check);
if ($numrows == 0 )
{
$InvalidMSG = "wrong";
trim($InvalidMSG);
$InvalidMSGJSon = json_encode($InvalidMSG);
echo($InvalidMSGJSon);
}
else {
//login user
$row = mysqli_fetch_array($check);
$mail = $row['mail'];
$array = array('mail' => $mail);
$answer = json_encode($array);//"Login Done," . $mail;
echo($answer);
}
mysqli_close($link);
import React from 'react';
import { StyleSheet, TouchableOpacity, TextInput, Text, View, Alert } from 'react-native';
import styles from './From.styles.js'
export default class Form extends React.Component {
constructor(props) {
super(props)
this.state = {
UserEmail: '',
UserPassword: ''
}
UserLoginFunction = () =>{
const { UserEmail } = this.state ;
const { UserPassword } = this.state ;
const url = 'https://andrianodna.000webhostapp.com/Login.php'
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: UserEmail,
password: UserPassword
})
})
.then((response) => response.text())
.then((responseJson) => {
if(responseJson === "wrong")
{
//Alert.alert('E-Mail o Contraseña equivocados')
Alert.alert(responseJson);
}
else{
this.props.navigation.navigate('Router' , { Email:UserEmail});
Alert.alert(responseJson);
}
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.container}>
<TextInput style ={styles.input}
placeholder = 'E-Mail'
onChangeText={UserEmail => this.setState({UserEmail})}
placeholderTextColor="rgba(0,0,0,0.3)"
keyboardType="email-address"
underlineColorAndroid='rgba(0,0,0,0)'/>
<TextInput style ={styles.input}
placeholder = 'Contraseña'
onChangeText={UserPassword => this.setState({UserPassword})}
placeholderTextColor="rgba(0,0,0,0.3)"
underlineColorAndroid='rgba(0,0,0,0)'
secureTextEntry = {true} />
<TouchableOpacity onPress={this.UserLoginFunction} style={styles.Button}>
<Text style={styles.buttonText}>{this.props.type}</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.Button2}>
<Text style={styles.buttonText}>{this.props.type2}</Text>
</TouchableOpacity>
</View>
);
}
}
userEmail和userPassword为我提供了我想要的值,我只是想在前面说过的状态错误时不去Router.js。我尝试使用\ n和\ r在不同的组合上。还有“==”而不是“===”。
答案 0 :(得分:1)
您从服务器("wrong"
)收到的值在开头有\r\n
,这就是条件失败的原因。我试过responseJson === '\r\n"wrong"'
(非常有线),条件是true
。请注意"
也是返回字符串的一部分。
希望这会有所帮助!