【问题标题】:React-native JSON Parse反应原生 JSON 解析
【发布时间】:2020-06-13 13:08:13
【问题描述】:

每次登录 JSON 解析错误时都会出错:无法识别的令牌 '

导出默认类表单扩展组件{

>constructor(props) {
>     super(props)
>     this.state = { email1: '', password1: '' }   }
> 
>   login = () => {
>     const { email1 } = this.state;
>     const { password1 } = this.state;
> 
>     fetch('http://192.168.1.114/login.php',{
>       method: 'POST',
>       headers:{
>         'Accept': 'application/json',
>         'Content-Type':'application/json'
>     },
>       body: JSON.stringify({
>         email: 'email1',
>         password: 'password1'
>       })
>     })
> 
>       .then((response) => response.json())            .then((responseJson) =>{
>               alert(responseJson);            })          .catch((error)=>{
>               console.error(error);           });
> 
>   };

【问题讨论】:

  • 这只是意味着 HTTP 响应不是 JSON。我们无法从您提供的代码中判断它是什么。

标签: react-native


【解决方案1】:

login.php 的代码

<?php


// Creating connection.
 $con = mysqli_connect('localhost','root','','test');

 // 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.
$email = $obj['email'];

// Populate Password from JSON $obj array and store into $password.
$password = $obj['password'];

//Applying User Login query with email and password match.
$Sql_Query = "select * from UserRegistrationTable where email = '$email' and password = '$password' ";

// Executing SQL Query.
$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 ;

 }

 mysqli_close($con);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多