I'm simply trying to get POST parameters on my PHP server, sent from my ReactJS app. In the server response, I can see that 'myParameter' is null. I think this is because of the Cross Origin issue, but I've tried many suggestions from answers to similar questions and nothing I've tried has worked. I believe that I'm looking for the proper code to add to my PHP file. It's been days now of this issue and I'm hoping someone here can help.
Background information (in case it matters at all). My PHP file is on my server hosted with hostgator. GET requests with URL parameters work perfectly, but POST parameters seem to be blocked. I'm trying to send more data than the URL option allows.
Thank you so much!
ReactJS Code:
axios.post('https://mywebsite.com/endpoint.php', {
myParameter: 'test',
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
PHP Code:
<?php
header("Access-Control-Allow-Origin: *");
echo json_encode($_POST['myParameter']);
?>
I also tried this, but it did not change the result:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Max-Age: 86400');
echo json_encode($_POST['myParameter']);
?>
EDIT:
Based on the answer from the post linked by Aaqib, I was able to successfully get the data on my server!