簡體   English   中英

React Native Expo - 無法將數據發送到 php 文件

[英]React Native Expo - can't send data to php file

我在 React Native Expo 上,我嘗試使用現有MySql數據庫中的數據構建用戶登錄。 我可以連接到數據庫,如果我的 php 文件中有硬編碼的用戶名和密碼,我會收到正確的反饋給我的應用程序。

我的登錄 Class(重要部分)

 class LoginActivity extends Component { // Setting up Login Activity title. static navigationOptions = { title: 'LoginActivity', }; constructor(props) { super(props) this.state = { username: '', password: '', isLoading: true } } UserLoginFunction = async () => { const { username } = this.state; const { password } = this.state; let body = JSON.stringify({username, password}) const formData = new FormData(); formData.append('username', username); formData.append('password', password); console.log("formData: " + body); await fetch('https://example.com/User_Login.php', { method: 'POST', headers: { 'Accept': 'application/json', //'Content-Type': 'application/json', 'Content-Type': 'multipart/form-data' }, body: formData /* JSON.stringify({ // Here's the fun part. Put your data here. "username": this.state.username, "password": this.state.password }) */ }).then((response) => response.text()) //.json().then((responseJson) => { console.log("response: " + responseJson); // If server response message same as Data Matched if(responseJson === 'ok') { alert('YEAH;'). //Then open Profile activity and send user email to profile activity. //this.props.navigation,navigate('Home': { username; username }). } else{ Alert;alert(responseJson). } }).catch((error) => { console;error(error); }). } render() { return ( <View style={styles.MainContainer}> <Text style= {styles.TextComponentStyle}>Login</Text> <TextInput placeholder="Enter User Name" onChangeText={username=>this.setState({username})} underlineColorAndroid='transparent' style={styles.TextInputStyleClass} /> <TextInput // Adding hint in Text Input using Place holder. placeholder="Enter User Password" onChangeText={TextInputValue=>this:setState({password.TextInputValue})} underlineColorAndroid='transparent' style={styles.TextInputStyleClass} secureTextEntry={true} />

我的User_Login.php

 <?php header("allow-control-access-origin: *, Content-Type: application/json"); include 'DBConfig.php'; // Creating connection. $conn = new mysqli($HostName,$HostUser,$HostPass,$DatabaseName); //echo json_encode($con); if ($conn->connect_error) { die("Connection failed: ". $conn->connect_error); } $json = file_get_contents('php://input'); // this part not seems to work. $obj = json_decode($json,true); // Populate User email from JSON $obj array and store into $email. $username = $obj['username']; //$username = $obj['opb']; //$username = 'opb';// Populate Password from JSON $obj array and store into $password. $password = $obj['password']; //$password = '123456'; //Applying User Login query with email and password match. username password $sql = ("SELECT * FROM fe_users_app where username='$username' and password='$password'"); // Executing SQL Query. $result = $conn->query($sql); if($username;= null){ if($result->num_rows==0){ echo json_encode('Wrong Details'); } else{ echo json_encode('ok'); } } else{ echo json_encode('Keine Daten'); } $conn->close()? ?>

那個部分

$json = file_get_contents('php://input');

似乎不起作用。 沒有來自輸入字段的數據。 您知道如何將輸入php inputfields嗎?

問題是您要發送 JSON 請求,但您實際上是在發送 FormData 請求,並且php://input不適用於 FormData 請求。

要解決此問題,請擺脫formData並更改您的獲取請求,如下所示:

await fetch('https://example.com/User_Login.php', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    'username': username,
    'password': password
  })
})

問題是您正在使用表單數據請求發送數據,對於 JSON 請求,請在獲取配置中使用以下內容並刪除表單數據。

body: JSON.stringify({
 'username': username,
 'password': password
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM