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 关于#GPU jetson#的pcie驱动开发问题,如何解决?
    • ¥15 stm32f103zet6 串口5无法收发数据
    • ¥15 关于C语言使用线程队列实现多线程并发
    • ¥15 这个运行,错误在哪里呀,大家看看吧,教导我
    • ¥15 advanceinstaller对话框设置
    • ¥100 正常上网,内部网页无法打开
    • ¥15 组件库引入并使用在若依框架未展示
    • ¥149 关于#使用python 的Flash Echarts+ajax+mysql动态数据实现饼图#的问题,请各位专家解答!
    • ¥15 RichTextBox中追加文本时报错
    • ¥15 关于c语言的学习问题