【问题标题】:Stuck in error JSON Parse error: Unable to parse JSON string陷入错误 JSON Parse 错误:无法解析 JSON 字符串
【发布时间】:2020-11-17 14:55:24
【问题描述】:

我陷入了非常常见的错误 JSON 解析错误无法确定 Web 服务有故障或无法获取代码 当我在我的邮递员中测试它时,我从这个 Web 服务中得到响应,它返回两个对象,但是当我想从中登录时Web服务总是给出解析错误

React Native Function to loginUser 可能存在错误

UserLoginFunction = () =>{
 const { UserContact }  = this.state ;
 const { UserPassword }  = this.state ;
 if(this.state.UserContact == ""){
   ToastAndroid.show('Pleas Enter Contact Number Correctly ',ToastAndroid.SHORT)
 }
 else if (this.state.UserPassword == ""){
   ToastAndroid.show('Please Enter Password Correctly',ToastAndroid.SHORT)
 }
 else{

fetch(urls.localhosturl + urls.login, { 
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
 
    user_contact: UserContact,
    user_password: UserPassword,
    //user_name: UserName,
 
  })
 
})
      
      .then((response) => response.json())
      .then((responseJson) => {

        console.log(responseJson)
        // If server response message same as Data Matched
         if(responseJson === 'Data Matched')
          {
            //Save User Details to Local Storage
            AsyncStorage.setItem("userContact", JSON.stringify(UserContact));
            //Then open Profile activity and send user email to profile activity.
            this.props.navigation.navigate('Home',{user_contact:UserContact,});

        }
        else{
          ToastAndroid.show(responseJson,ToastAndroid.SHORT);
          //Alert.alert(string,responseJson);
          
        }

      }).catch((error) => {
        console.error(error);
      });
 
    }
  }

PHP 网络服务

<!-- begin snippet: js hide: false console: true babel: false -->

<?php

// Importing DBConfig.php file.
include 'config.php';
// Creating connection.
$con = mysqli_connect($host_name, $host_user, $host_password, $database_name);
// 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);

$user_contact = $obj['user_contact'];
$user_password = $obj['user_password'];

// $user_contact = $_REQUEST['user_contact'];
// $user_password = $_REQUEST['user_password'];
//$user_name = $obj['user_name'];

$Sql_Query = "select * from user_information where user_contact = '$user_contact' and user_password = '$user_password' ";
$check = mysqli_fetch_array(mysqli_query($con, $Sql_Query));

if (isset($check)) {
    $SuccessLoginMsg = 'Data Matched';
    // Converting the message into JSON format.
    $SuccessLoginJson = json_encode($SuccessLoginMsg);
    // Echo the message.
    echo $SuccessLoginJson;
} else {
    // If the record inserted successfully then show the message.
    $InvalidMSG = 'Invalid Username or Password Please Try Again';
    // Converting the message into JSON format.
    $InvalidMSGJSon = json_encode($InvalidMSG);
    // Echo the message.
    echo $InvalidMSGJSon;
}

$result = $con->query($Sql_Query);
$array = $result->fetch_assoc();
$json = json_encode($array, true);

echo $json;

mysqli_close($con);

【问题讨论】:

  • “数据匹配”正在破坏 json,看起来有人在其中留下了一些调试代码。糟糕!服务似乎有问题。
  • @rjdown 有什么解决办法

标签: java php json react-native


【解决方案1】:

看起来您的 PHP 服务的标准输出被用作 JSON 的源。

在这种情况下,您吐出以下行的事实会给您带来麻烦:

echo $SuccessLoginJson;

您可能希望抑制此消息,将其写入日志文件,或者将其写入标准错误。无论如何,您已经将您的 Web 服务变成了给您的客户端提供两个结果而不是一个的东西,而客户端不知道如何处理它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多