繁体   English   中英

卡在错误JSON Parse error: Unable to parse JSON string

[英]Stuck in error JSON Parse error: Unable to parse JSON string

我陷入了非常常见的错误 JSON 解析错误无法弄清楚 web 服务有故障或获取代码我从这个 web 服务得到响应当我在我的 postman 中测试它时它返回两个对象但是当我想从这个 web 服务登录时总是给出解析错误错误截图

来自 RAW INPUT 的 PostMan 响应

将本机 Function 反应到可能存在错误的登录用户

 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);

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

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

echo $SuccessLoginJson;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM