Hello I am trying to send post variables to my API and I not getting the post data in the PHP file
This is my react native code:
let data = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
'firstname': 'test',
}),
}
fetch(GLOBALS.API + '/registerApi.php?key=' + GLOBALS.KEY, data)
.then((response) => response.json())
.then((responseJson) => {
Alert.alert(
'Alert Title',
responseJson.output
)
})
.catch((error) => {
console.error(error);
});
It's returning me empty: []
$array = array(
"output" => json_encode($_POST)
);
$output = json_encode($array);
die($output);
When I use $_REQUEST it's returning me only the key get parameter without the firstname one.
JSON.stringify did not work for me, try using FormData instead to prepare data for sending. Here is example:
import FormData from 'FormData';
let formData = new FormData();
formData.append('firstname', 'test');
let data = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: formData
}
fetch(api_url, data)
.then(response => response.json())
.then(responseJson => console.log('response:', responseJson))
.catch(error => console.error(error));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With