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

在本机中发送和获取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条回答 默认 最新

  • dongzhiqi0332 2019-06-20 14:26
    关注

    I think the issue is you passing , in complete your JSON object please remove that check below code.

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

    , it will take your third argument your navigation Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿