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

登录 React Native MySQL

登录 React Native MySQL

慕姐8265434 2022-07-08 17:40:55
我们的登录代码有问题。我们不断收到 SyntaxError: JSON Parse Error:我们缩小了在 .then(response) 行之一或 php 代码中发生的响应错误。我不确定我在这里做错了什么。有什么帮助吗?!loginScreen.jslogin = () =>{    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'];
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

我认为您需要尝试更改代码

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

到您的 ip,使用ipconfig检查您的本地 ip 并像这样更改代码

fetch('http://192.168.1.1/login.php'....

示例 ip


查看完整回答
反对 回复 2022-07-08
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

据我所知,问题是您的 PHP 服务器没有返回有效的 json。它返回一个字符串,即使您对字符串进行 json_encode,该字符串也将是"Data Matched"或"Invalid Username or Password Please Try Again"。但是,当您发出 fetch 请求并执行


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

          .then((responseJson) => {

response.json()).then((responseJson)当您执行.json() 部分尝试解析无效 JSON 的字符串时,它会尝试解析字符串“数据匹配” 。因此,您可以通过两种方式解决这个问题,实际上从 php 服务器发回有效的 json 构建这样的对象{ "success" : true, message :"Data Matched"}或简单地删除 .json() 链,因此 javascript 将返回一个字符串,而不是尝试将其解析为 JSON。所以没有 .json() 它将是


...

.then((response) => {

           // If server response message same as Data Matched

           if(response === 'Data Matched')

            {

             alert(response);

            } else {

              alert(response);

            }

...

只需先尝试删除.then((response) => response.json()第 58 行的部分,然后直接跳到下一个链接的 then 子句


查看完整回答
反对 回复 2022-07-08
  • 2 回答
  • 0 关注
  • 180 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

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

帮助反馈 APP下载

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

公众号

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