You are facing a CORS origin policy issue. To solve this problem, you need access rights to the server-side API. In particular, you need to add a line in the header of php or another server endpoint:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin: http://example.com');
$json = file_get_contents('php://input');
$jsonObj = json_decode($json);
print_r($jsonObj->message);
...
?>
Also, make sure NOT in the server endpoint header:
header("Access-Control-Allow-Credentials" : true);
The working sample code model with the POST request:
const data = {
optPost: 'myAPI',
message: 'We make a research of fetch'
};
const endpoint = 'http://example.com/php/phpGetPost.php';
fetch(endpoint, {
method: 'POST',
body: JSON.stringify(data)
})
.then((resp) => resp.json())
.then(function(response) {
console.info('fetch()', response);
return response;
});
source
share