亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

登錄 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 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號