dongtaoxue4674 2019-07-06 16:49
浏览 90

与PHP的React-native总是返回NULL

I am trying to create a login that uses PHP/MySQL authentication for my app. Someone actually had the exact same post that I am having. How can I use react-native with php with fetch return data is always null I tried implementing the recommended changes because I had not heard about php://input until today but it is still returning NULL. I wanted to see if maybe the handshake was 'hiccuping' so I configured a test table built in MySQL to store the raw data that is returned to see if the server is even receiving the information, but the table column is always returning NULL after each insert. I'm trying to pass JSON data to the PHP script but I guess it doesn't like it maybe? Does anyone have any ideas?

// My code for my login.js file

import React from "react";
import { StyleSheet, ScrollView, KeyboardAvoidingView, TouchableOpacity, AsyncStorage, TextInput, Button, View, Text } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';

// internal includes...
import styles from '../styles/styles.js'; // styles is the only one lowercase

class Login extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      username: '',
      password: '',
    }
  }
  // check if the user is already logged in...
  componentDidMount() {
    this._loadInitialState().done();
  }

  _loadInitialState = async () => {
    var value = await AsyncStorage.getItem('user');
    if (value !== null) {
      this.props.navigation.navigate('Profile');
    }
  }

  render() {
    return (
        <KeyboardAvoidingView behavior='padding' style={styles.wrapper}>
          <View style={styles.container}>
            <Text style={styles.header}> Login </Text>

            <TextInput
              style={styles.TextInput} placeholder='Username'
              onChangeText={ (username) => this.setState({username}) }
              underLineColorAndroid='transparent'
            />
            <TextInput
              style={styles.TextInput} placeholder='Password'
              onChangeText={ (password) => this.setState({password}) }
              underLineColorAndroid='transparent'
            />
            <TouchableOpacity
              style={styles.btn}
              onPress={this.login}>
              <Text> Log In </Text>
            </TouchableOpacity>

          </View>
        </KeyboardAvoidingView>
    );
  }

  login = () => {
    // check if the username is being passed off properly...
    //alert(this.state.username);
    fetch('MYWEBSITELINK', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username: this.state.username,
        password: this.state.password,
      })
    })

    .then(function(response){ return response.json(); }) // transforms response into data that is readable for this app...
    .then(function(data) {
        console.log(data);
    })
  }
}
export default Login

// the php file that handles a login request

<?php
// Access MySQL login information file...
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/live/configuration/mysqli.php";
require_once($path);

// set this in all mobile related files...
header('Content-Type: application/json');




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


// create user record in the login table...
$query = "INSERT INTO test (data)
VALUES (?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $json);
$stmt->execute();
$stmt->close();



?>

Edit 1: I forgot to post an error/warning that I'm receiving which I think is related to my JSON format. "[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected EOF]". Does anyone now any way to output the response I sent in the console so I can see if it needs reformatted? Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥50 汇编语言除法溢出问题
    • ¥65 C++实现删除N个数据列表共有的元素
    • ¥15 Visual Studio问题
    • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波