React native with PHP始终返回NULL

React native with PHP始终返回NULL,php,json,react-native,xmlhttprequest,fetch,Php,Json,React Native,Xmlhttprequest,Fetch,我正在尝试为我的应用程序创建一个使用PHP/MySQL身份验证的登录名。 实际上有人和我的帖子完全一样。 我尝试实施建议的更改,因为我没有听说过php://input 直到今天,它仍然返回空值。我想看看握手是否“打嗝”,所以我配置了一个在MySQL中构建的测试表来存储返回的原始数据,以查看服务器是否接收到信息,但每次插入后表列总是返回NULL。我试图将JSON数据传递给PHP脚本,但我猜它可能不喜欢它?有人有什么想法吗 //我的login.js文件的代码 import React from "r

我正在尝试为我的应用程序创建一个使用PHP/MySQL身份验证的登录名。 实际上有人和我的帖子完全一样。 我尝试实施建议的更改,因为我没有听说过php://input 直到今天,它仍然返回空值。我想看看握手是否“打嗝”,所以我配置了一个在MySQL中构建的测试表来存储返回的原始数据,以查看服务器是否接收到信息,但每次插入后表列总是返回NULL。我试图将JSON数据传递给PHP脚本,但我猜它可能不喜欢它?有人有什么想法吗

//我的login.js文件的代码

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
从“React”导入React;
从“react native”导入{样式表、ScrollView、键盘AvoidingView、TouchableOpacity、AsyncStorage、TextInput、按钮、视图、文本};
从“react navigation”导入{createStackNavigator,createAppContainer};
//内部包括。。。
从“../styles/styles.js”;/”导入样式styles是唯一一个小写的
类登录扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
用户名:“”,
密码:“”,
}
}
//检查用户是否已登录。。。
componentDidMount(){
这是。_loadInitialState().done();
}
_loadInitialState=async()=>{
var value=await AsyncStorage.getItem('user');
如果(值!==null){
this.props.navigation.navigate('Profile');
}
}
render(){
返回(
登录
this.setState({username})}
“透明的”
/>
this.setState({password})}
“透明的”
/>
登录
);
}
登录=()=>{
//检查用户名是否正确传递。。。
//警报(this.state.username);
获取('MYWEBSITELINK'{
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify({
用户名:this.state.username,
密码:this.state.password,
})
})
.then(函数(response){return response.json();})//将响应转换为此应用程序可读的数据。。。
.then(功能(数据){
控制台日志(数据);
})
}
}
导出默认登录名
//处理登录请求的php文件

<?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();



?>


编辑1:我忘记发布我收到的错误/警告,我认为这与我的JSON格式有关。“[未处理的承诺拒绝:语法错误:JSON解析错误:意外的EOF]”。现在是否有人能够输出我在控制台中发送的响应,以便我查看是否需要重新格式化?谢谢。

尝试在get方法中传递它,以检查连接、数据是否正常。如果Get有效,那么问题在于如何在发帖前对数据进行字符串化。@JosepVidal我认为我的数据有问题。我刚刚注意到,它在expo控制台中为我的物理iphone设备返回了一条警告:“未处理的承诺拒绝:SyntaxError:JSON解析错误:意外的EOF]”。。。所以我的问题是,我不确定它的确切发送格式。有没有一种方法可以输出身体的反应,以查看发送的是什么?我想JSON.stringify会为我处理好格式。试着在get方法中传递它,检查连接、数据是否正常。如果Get有效,那么问题在于如何在发帖前对数据进行字符串化。@JosepVidal我认为我的数据有问题。我刚刚注意到,它在expo控制台中为我的物理iphone设备返回了一条警告:“未处理的承诺拒绝:SyntaxError:JSON解析错误:意外的EOF]”。。。所以我的问题是,我不确定它的确切发送格式。有没有一种方法可以输出身体的反应,以查看发送的是什么?我想JSON.stringify会为我处理好格式。