douwen5690
2019-06-20 13:31
浏览 172
已采纳

在本机中发送和获取JSON数据[JSON解析错误]

I have 2 screens; [1st screen - here user enters mobile number] this mobile number should pass to JSON and verify this mobile number is exist or not if the mobile number exists then it responds with some data. this response should pass to the next screen.

This is my 1st screen code [where user enter data]-

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

My PHP file [place on live server]

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

Error is ==>

JSON parse error : Unable to parse JSON string

NOTE ==> when I test PHP file on web browser there is no error in the PHP file but when I fetch this file to react native it causes the error!

enter image description here

1条回答 默认 最新

相关推荐 更多相似问题