猿问

发布到数据库不起作用(错误/警告:JSON 输入意外结束)

我正在使用 React Native 开发移动应用程序。问题是我似乎无法将任何内容插入数据库。我有一个注册页面,用户需要在其中编写用户名和密码(并重复该密码),几个月前该页面运行良好。我没有更改该特定页面上的任何内容,但是当我开始添加新页面时,例如另一个不起作用的页面,它停止工作并给了我可以在此处看到的警告:

另一个不起作用的页面是用户可以在其中发布带有标题日期等的工作。此页面尚未完成,但应该已完成,以便用户可以将某些内容发布到数据库,但它给了我可以在此处看到的错误:

http://img1.mukewang.com/6342915c0001b52506571160.jpg

注册页面的代码如下(不包括样式表):


import React, { Component } from "react"; 

import { View, Text, StyleSheet, Alert, TextInput, ImageBackground, Image, AsyncStorage, TouchableOpacity } from "react-native";

import { Button, Icon, withTheme } from 'react-native-elements';


const SIGNUP_URL = 'http://kamilla-server.000webhostapp.com/app/signUp.php';


class SignUp extends Component {

 constructor(props) {

    super(props);


    this.state= {

        email: '',

        password: '',

        password2: '',

    };

 }


async onSignUp() {

    const { email, password, password2 } = this.state;


    if(this.state.email != '' && this.state.password != '' && this.state.password2 != '') {

        if(password != password2) {

            Alert.alert('Password matcher ikke', 'De to indtastede passwords skal være det samme password')

        } else {

            const response = await fetch(SIGNUP_URL, {

                headers: {

                    Accept: 'application/json',

                    'Content-Type': 'application/json'

                },

                method: 'POST',

                body: JSON.stringify({ email, password }),

            })


            const data = await response.json()



            if (data.error) {

                alert(data.error)

            } else {

                AsyncStorage.setItem('UserID', data.user.UserID)

                this.props.navigation.navigate('SignUpMessage')

            }

        }

    } else {

        Alert.alert('Tomme felter', 'Venligt indtast email og password for at kunne oprette en bruger')

    }

}


翻阅古今
浏览 194回答 2
2回答

杨魅力

问题是这一行: $_SESSION['userID'] = $getUser['UserID']; The $getUserneeds to be $getNewUser,因为这是我从中获取的变量的名称。我一定是在不知不觉中改变了它,从而产生了问题。

回首忆惘然

我在这里可能错了,但我最好的猜测是您的 fetch api 收到错误,您应该尝试将async onSignUp函数的 fetch 修改为:&nbsp; await fetch(SIGNUP_URL, {<params>})&nbsp; &nbsp; .then(res => {&nbsp; &nbsp; &nbsp; // Handle API Errors&nbsp; &nbsp; &nbsp; if (!res.ok) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(res.statusText);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // Return if no errors&nbsp; &nbsp; &nbsp; return res.json();&nbsp; &nbsp; })&nbsp; &nbsp; // this is the data you want&nbsp; &nbsp; .then(data => data)&nbsp; &nbsp; // it will only reject on network failure or if anything prevented the request from completing&nbsp; &nbsp; .catch(error => {&nbsp; &nbsp; &nbsp; console.log(error.message)&nbsp; &nbsp; });您可以尝试上述方法,看看您是否在响应中收到错误!希望这可以帮助!!
随时随地看视频慕课网APP
我要回答