I'm new to react native trying to do the http request using fetch method (also tried axios). I'm using PHP as backend. What I'm trying is to print the JSON object returned from server in react native.
I'm sure that the response is returning from server cause I printed the result and it is a JSON array. But when I consoled the responseData
using Chrome dev tools I got this []
. I don't know where the mistake is.
index.js
componentDidMount(){
fetch('http://xxxxxxxxxx/result.php', {
method: 'GET',
}).then(response =>response.json())
.then((responseData) =>
{
console.log(responseData);
this.setState({
user: (responseData.name),
loaded:true,
})
});
}
render(){
return(
<Card>
<CardTitle style={styles.containerStyle} title={this.state.user} />
</Card>
);
}
}
Also my php is script is as follows
result.php
<?php
include 'db.php';
$check_json = file_get_contents('php://input');
$obj1= json_decode($check_json);
$uid =$obj1->{'uuid'};
$blood = $obj1->{'bloodgroup'};
$loc = $obj1->{'place'};
$prep =$mysqli->prepare("select token,name,uuid from signup where location
like '".$loc."%' and bloodgroup= ?");
$prep->bind_param("s",$blood);
$prep->execute();
$result= $prep->get_result();
$payload= array();
while($row= $result->fetch_array(MYSQL_ASSOC)){
$payload[]= array('name' =>$row['name']
'uuid' =>$row['name']);
}
$obj2 =json_encode($payload);
echo $obj2;
?>
and the response from server is
[{"name":"angel","uuid":"b4b8a266-60cf-4e30-a778-596316d1a4a2"}]