I have created a React application from which I am calling my server built on PHP.
The following is how I call my PHP file:
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: JSON.stringify({ name, username, password }),
};
console.log(requestOptions);
fetch('http://localhost/crud/requests/signup.php', requestOptions)
.then(res => res.json())
.then(data => console.log(data));
Here is what I have in the PHP file:
if (isset($_POST) && !empty($_POST)) {
// do something
}
When I print the $_POST variable, I get an empty array. Even the $_RESPONSE is empty.
But when I tried to print the input stream like this:
print_r(file_get_contents('php://input'));
Everything seems to be fine. Can anyone explain why does this happen? I tried to read it in the documentation & looked up on some forums and blogs but wasn't satisfied with the answers.