I'm trying to send API response from my Javascript file like this:
try {
await fetch('https://example.com/api', {
method: 'post',
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'john',
})
}).then((response) => response.json())
.then((data) => console.log(data));
} catch (e) {
console.error(e);
}
Now I'm trying to get the body of the request.
My Backend Server built on PHP.
I have already seen this Stackoverflow Question.
As I saw there, I need to do this:
To access the entity body of a POST or PUT request (or any other HTTP method):
$entityBody = file_get_contents('php://input');
This is my Index.php
file:
<?php
$entityBody = file_get_contents('php://input');
echo json_encode($entityBody);
I'm new to PHP so maybe its a stupid mistake.
My goal is to print john
in the console.