使用php json和react native获取数据时出现问题

使用php json和react native获取数据时出现问题,php,mysql,json,react-native,Php,Mysql,Json,React Native,我目前正在react native中开发一个简单的登录函数,它使用php和json从mysql获取数据。 我面临的问题是,我一次又一次地遇到“JSON解析错误:意外的EOF”,我不知道现在该怎么办 我相信问题一定出在react原生组件中,因为我已经在Postman上测试了PHP文件,并且得到了我想要的东西 出于某种原因,这篇文章被认为是一个关于sql InyActions的问题的可能重复,而这不是我要问的 以下是我编写的代码: 反应本机: constructor(props) { s

我目前正在react native中开发一个简单的登录函数,它使用php和json从mysql获取数据。 我面临的问题是,我一次又一次地遇到“JSON解析错误:意外的EOF”,我不知道现在该怎么办

我相信问题一定出在react原生组件中,因为我已经在Postman上测试了PHP文件,并且得到了我想要的东西

出于某种原因,这篇文章被认为是一个关于sql InyActions的问题的可能重复,而这不是我要问的

以下是我编写的代码:

反应本机:

  constructor(props) {
    super(props);
    this.state = { 
      userCI:'',
      userPassword:''

    };
  }


  login = () => {
    // Get data from PHP endpoint
    const {userCI} = this.state;
    const {userPassword} = this.state;

    fetch('http://192.168.1.14/dashboard/endpoint.php', {
      method:'POST',
      headers: {
          'Accept': 'application/json; text/plain; */*',
          'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        ci: userCI,
        password: userPassword,
        key:'test',
      })

    })
    .then(response => response.json())
    .then(response => {
      alert(response.message);
    })
    .catch(error =>{
      console.error(error);
    })
    .done();
  }

  render() {
    return (
      <View>
        <TextInput
          style={styles.Input}
          placeholder={'Usuario'}
          onChangeText= {
            userCI => this.setState({userCI})
          }
        />
        <TextInput 
          style={styles.Input}
          secureTextEntry={true} 
          placeholder={ 'Contraseña'} 
          onChangeText= {
            userPassword => this.setState({userPassword})
          }
        />
        <Button 
          style={styles.button} 
          onPress={this.login} 
          title={'Conectarse'} 
        />
      </View>

    );

  }
}
拜托,如果有人能帮我,我会很高兴的。
谢谢

这段代码非常不安全。使用准备好的语句,不要使用MD5作为密码。使用php本机函数密码\u hashI不使用react,但您确定需要执行
response.json()
?此外,只有当其中一个字段(ci或密码)为空时,您才会拥有
message
属性,但在所有情况下,您都试图
提醒它。@Dharman谢谢!我会努力做到的!你还有什么建议可以提高我在PHP上的安全性吗?@PatrickQ好吧,这是正确的,我没有注意到。你认为如果我在
if{}Else{}
语句中编写
alert
和来自json的其他响应,它会工作吗?这段代码非常不安全。使用准备好的语句,不要使用MD5作为密码。使用php本机函数密码\u hashI不使用react,但您确定需要执行
response.json()
?此外,只有当其中一个字段(ci或密码)为空时,您才会拥有
message
属性,但在所有情况下,您都试图
提醒它。@Dharman谢谢!我会努力做到的!你还有什么建议可以提高我在PHP上的安全性吗?@PatrickQ好吧,这是正确的,我没有注意到。你认为如果我在
if{}Else{}
语句中编写
alert
和来自json的其他响应,它会工作吗?

    class User {
        private $db;
        private $connection;
        function __construct() {
            $this->db = new DB_Connection();
            $this->connection = $this->db->get_connection();
        }
        public function does_user_exist($ci, $password){
            $query = "SELECT * FROM users WHERE ci='$ci' AND password='$password'";
            $result = mysqli_query($this->connection, $query);
            if(mysqli_num_rows($result) > 0){
               $json['success'] = 'Bienvenido';
               echo json_encode($json);
               mysqli_close($this->connection);
            }else{
                $json['error'] = 'Wrong password';
                echo json_encode($json);
                mysqli_close($this->connection);
            }
        }
    }

    $user = new User();
    if(isset($_POST['ci'],$_POST['password'])) {
        $ci = $_POST['ci'];
        $password = $_POST['password'];
        if (!empty($ci) && !empty($password)) {
            $encrypted_password = md5($password);
            $user->does_user_exist($ci, $password);
        } else {
            echo json_encode(array(
                'message' => 'There was a unexpected error',
            ));
        }
    }