为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
翻阅古今 2022-10-09 17:15:28
我正在使用 React Native 开发移动应用程序。问题是我似乎无法将任何内容插入数据库。我有一个注册页面,用户需要在其中编写用户名和密码(并重复该密码),几个月前该页面运行良好。我没有更改该特定页面上的任何内容,但是当我开始添加新页面时,例如另一个不起作用的页面,它停止工作并给了我可以在此处看到的警告:另一个不起作用的页面是用户可以在其中发布带有标题日期等的工作。此页面尚未完成,但应该已完成,以便用户可以将某些内容发布到数据库,但它给了我可以在此处看到的错误:注册页面的代码如下(不包括样式表):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')    }}
查看完整描述

2 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

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



查看完整回答
反对 回复 2022-10-09
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

我在这里可能错了,但我最好的猜测是您的 fetch api 收到错误,您应该尝试将async onSignUp函数的 fetch 修改为:


  await fetch(SIGNUP_URL, {<params>})

    .then(res => {

      // Handle API Errors

      if (!res.ok) {

        console.log(res.statusText);

      }

      // Return if no errors

      return res.json();

    })

    // this is the data you want

    .then(data => data)

    // it will only reject on network failure or if anything prevented the request from completing

    .catch(error => {

      console.log(error.message)

    });

您可以尝试上述方法,看看您是否在响应中收到错误!希望这可以帮助!!


查看完整回答
反对 回复 2022-10-09
  • 2 回答
  • 0 关注
  • 193 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号