PHP的React-native始终返回NULL

时间:2019-07-06 16:49:07

标签: php json react-native xmlhttprequest fetch

我正在尝试创建一个对我的应用程序使用PHP / MySQL身份验证的登录名。 有人实际上拥有与我完全相同的职位。 How can I use react-native with php with fetch return data is always null 我尝试实施建议的更改,因为直到今天我还没有听说过php:// input,但是它仍然返回NULL。我想看看握手是否在打h,所以我配置了一个内置于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

//处理登录请求的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格式有关的错误/警告。 “ [[未处理的承诺拒绝:SyntaxError:JSON解析错误:意外的EOF]”。现在是否有人以任何方式输出我在控制台中发送的响应,以便查看是否需要重新格式化?谢谢。

0 个答案:

没有答案