登录 React Native MySQL

我们的登录代码有问题。我们不断收到 SyntaxError: JSON Parse Error:

http://img4.mukewang.com/62c7fbc40001f7db03680620.jpg

我们缩小了在 .then(response) 行之一或 php 代码中发生的响应错误。我不确定我在这里做错了什么。有什么帮助吗?!


loginScreen.js


login = () =>{

    const { UserEmail }  = this.state ;

    const { UserPassword }  = this.state ;


    fetch('http://localhost:65535/login.php', {

      method: 'POST',

      headers: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

      },

      body: JSON.stringify({

        user_email: UserEmail,

        user_pass: UserPassword

      })

     //Error within line 59-61 or php

    })

    .then((response) => response.json())

    .then((responseJson) => {

        // If server response message same as Data Matched

        if(responseJson === 'Data Matched'){

            alert("Correct");     

        } else{

            alert("Incorrect");

        }


     }).catch((error) => {

         console.error(error);

     });


}



  render() {

    return (

      <View style={styles.container}>

        <ScrollView

          style={styles.container}

          contentContainerStyle={styles.contentContainer}>

          <View style={styles.welcomeContainer}>

            <Image

              source={

                __DEV__

                  ? require('../assets/images/HootLogo.png')

                  : require('../assets/images/robot-prod.png')

              }

              style={styles.welcomeImage}

            />

          </View>

login.php 似乎一切都正确布局并且可以正常工作。我尝试将 ' 更改为 ` 和所有内容。


<?php


 // Importing DBConfig.php file.

 include 'DBConfig.php';


 // Creating connection.

 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);


 // Getting the received JSON into $json variable.

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


 // decoding the received JSON and store into $obj variable.

 $obj = json_decode($json,true);


 // Populate User email from JSON $obj array and store into $email.

 $user_email = $obj['user_email'];


 // Populate Password from JSON $obj array and store into $password.

 $user_pass = $obj['user_pass'];


慕姐8265434
浏览 181回答 2
2回答

喵喵时光机

我认为您需要尝试更改代码fetch('http://localhost:65535/login.php'....到您的 ip,使用ipconfig检查您的本地 ip 并像这样更改代码fetch('http://192.168.1.1/login.php'....示例 ip

慕桂英3389331

据我所知,问题是您的 PHP 服务器没有返回有效的 json。它返回一个字符串,即使您对字符串进行 json_encode,该字符串也将是"Data Matched"或"Invalid Username or Password Please Try Again"。但是,当您发出 fetch 请求并执行.then((response) => response.json())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then((responseJson) => {response.json()).then((responseJson)当您执行.json() 部分尝试解析无效 JSON 的字符串时,它会尝试解析字符串“数据匹配” 。因此,您可以通过两种方式解决这个问题,实际上从 php 服务器发回有效的 json 构建这样的对象{ "success" : true, message :"Data Matched"}或简单地删除 .json() 链,因此 javascript 将返回一个字符串,而不是尝试将其解析为 JSON。所以没有 .json() 它将是....then((response) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// If server response message same as Data Matched&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(response === 'Data Matched')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }...只需先尝试删除.then((response) => response.json()第 58 行的部分,然后直接跳到下一个链接的 then 子句
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript