发送和获取JSON数据以响应本机[JSON解析错误]

时间:2019-06-20 13:31:57

标签: php json react-native fetch-api

我有2个屏幕; [第一个屏幕-用户在此输入手机号码] 该手机号码应传递给JSON,并验证该手机号码是否存在(如果该手机号码存在),然后它会响应一些数据。 该响应应传递到下一个屏幕。

这是我的第一个屏幕代码 [用户输入数据的地方]-

class Register extends React.Component {
constructor(props) { 
    super(props) 
    this.state = {

    abc: ''
    } 
  }

  UserLoginFunction = () =>{

 const { abc } = this.state;

fetch('http://demo.weybee.in/react/User_Login.php', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({

  mobileno: abc,

      })

}).then((response) => response.json())
      .then((responseJson) => {
        // If server response message same as Data Matched
       if(responseJson != 'Enter valid phone number' )
        {   

            console.log(responseJson[0]);
            console.log(responseJson[1]);
            console.log(responseJson[2]);
            console.log(responseJson[3]);
            //Then open Profile activity and send user email to profile activity.
            this.refs.toast.show('Login successful', 500, () => {
            const { navigation } = this.props;

            const { abc }  = this.state ;

            navigation.navigate("Profile",
              {
                mobileno : abc, 
                myJSON: responseJson[0]+ " " +responseJson[1],
                myJSON2: responseJson[2],
                myJSON3: responseJson[3], },
              );
    });
        }
        else{

          Alert.alert(responseJson);
        }

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

  }
  render() {

    return (
<Block width={width * 0.8} style={{ marginBottom: 20, marginTop: 20 }}>
                      <Input
                        style={{borderRadius:50, borderWidth:5}}
                        onChangeText={abc => this.setState({abc})}
                        borderless
                        placeholder="Enter Mobile number"
                        keyboardType={'phone-pad'}
                        iconContent={
                          <Icon
                            size={16}
                            color={argonTheme.COLORS.ICON}
                            name="nav-right"
                            family="ArgonExtra"
                            style={styles.inputIcons}
                          />
                        }
                      />
                    </Block>

                    <Block middle>
                      <Button color="primary" style={styles.createButton} 
                       onPress={this.UserLoginFunction}>
                        <Text bold size={14} color={argonTheme.COLORS.WHITE}>
                          Log In
                        </Text>
                      </Button>
                      <Toast ref="toast"
                      style={{backgroundColor:'#131313'}}
                      textStyle={{color:'white',fontWeight: 'bold'}}
                      position='top'/>
                    </Block>
    );
  }
}

enter image description here

我的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 column from JSON $obj array and store into $coulmn.
$mobileno = $obj['mobileno'];
//Applying User Login query with mobile number match.
$Sql_Query = "select firstname,lastname,email,profession,mobileno from member where mobileno = '$mobileno' ";

// Executing SQL Query.
$check = mysqli_fetch_array(mysqli_query($con,$Sql_Query));
$VMNO = $mobileno;
echo $VMNO;

if(isset($check)){


 // $SuccessLoginMsg = 'Data Matched';


 // Converting the message into JSON format.
$SuccessLoginJson = json_encode($SuccessLoginMsg);


$check = json_encode($check);
// Echo the message.
 echo $check ; 
 }

 else{

 // If the record inserted successfully then show the message.
$InvalidMSG = 'Enter valid phone number' ;

// Converting the message into JSON format.
$InvalidMSGJSon = json_encode($InvalidMSG);

// Echo the message.
 echo $InvalidMSGJSon ;

 }

 mysqli_close($con);
?>

错误是==>

JSON解析错误:无法解析JSON字符串

注意==> ,当我在网络浏览器上测试PHP文件时,PHP文件中没有错误 但是当我获取此文件以进行本地响应时,会导致错误!

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为问题在于您通过了,请在完整的JSON对象中传递该代码,然后将其删除。

 navigation.navigate("Profile",{
  mobileno : abc, 
  myJSON: responseJson[0]+ " " +responseJson[1],
  myJSON2: responseJson[2],
  myJSON3: responseJson[3], 
});

,导航时将使用您的第三个参数 谢谢