Php 在react native[JSON解析错误]中发送和获取JSON数据
我有两个屏幕;[第一屏-用户在此输入手机号码] 该手机号码应传递给JSON,并验证该手机号码是否存在。如果该手机号码存在,则会使用一些数据进行响应。 此响应应传递到下一屏幕 这是我的第一个屏幕代码[用户输入数据的地方]-Php 在react native[JSON解析错误]中发送和获取JSON数据,php,json,react-native,fetch-api,Php,Json,React Native,Fetch Api,我有两个屏幕;[第一屏-用户在此输入手机号码] 该手机号码应传递给JSON,并验证该手机号码是否存在。如果该手机号码存在,则会使用一些数据进行响应。 此响应应传递到下一屏幕 这是我的第一个屏幕代码[用户输入数据的地方]- class Register extends React.Component { constructor(props) { super(props) this.state = { abc: '' } } UserLoginFun
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>
);
}
}
类寄存器扩展React.Component{
构造器(道具){
超级(道具)
此.state={
abc:'
}
}
UserLoginFunction=()=>{
const{abc}=this.state;
取('http://demo.weybee.in/react/User_Login.php', {
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify({
美国广播公司,
})
}).then((response)=>response.json())
.然后((responseJson)=>{
//如果服务器响应消息与数据匹配
如果(responseJson!=“输入有效电话号码”)
{
console.log(responseJson[0]);
console.log(responseJson[1]);
console.log(responseJson[2]);
console.log(responseJson[3]);
//然后打开配置文件活动并向配置文件活动发送用户电子邮件。
this.refs.toast.show('Login successful',500,()=>{
const{navigation}=this.props;
const{abc}=this.state;
导航。导航(“配置文件”,
{
美国广播公司,
myJSON:responseJson[0]+“”+responseJson[1],
myJSON2:responseJson[2],
myJSON3:responseJson[3],},
);
});
}
否则{
警惕,警惕(responseJson);
}
}).catch((错误)=>{
控制台错误(error);
});
}
render(){
返回(
this.setState({abc})}
无边界
占位符=“输入手机号码”
键盘类型={'phone-pad'}
图标内容={
}
/>
登录
);
}
}
我的PHP文件[放置在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);
?>
错误为==>
JSON分析错误:无法分析JSON字符串
注意==>当我在web浏览器上测试PHP文件时,PHP文件中没有错误
但是,当我获取此文件时,它会导致错误
我认为问题在于您传递了,在完成JSON对象时,请删除代码下面的检查
navigation.navigate("Profile",{
mobileno : abc,
myJSON: responseJson[0]+ " " +responseJson[1],
myJSON2: responseJson[2],
myJSON3: responseJson[3],
});
,您的第三个参数将用于导航
谢谢