Overview
My react app is hosted on githup-pages and a PHP API is hosted on a standard, paid hosting.
This works (GET)
When I started developping my app, I encountered a pretty popular error, which is Access to fetch at ... has been blocked by CORS policy. I have found a quick-fix which was to write this header("Access-Control-Allow-Origin: *") in the PHP's first lines and it worked. However those were the GET requests.
This doesn't work (POST)
This time I have a piece of code in which I have to use a POST request and need to access it's response. This is the piece of code responsible for sending the post request:
const editEvent = async () => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', },
body: JSON.stringify({ eventID: chosenEvent, courseID: courseID, groupID: groupID, time:time, date: date, description: description, typeID: typeID, password: password}),
mode: 'cors',
};
const response = await fetch(`https://aleksanderblaszkiewicz.pl/kiedykolos/edit_event.php`, requestOptions);
}
It happened again - I am getting same error Access to fetch at ... has been blocked by CORS policy. Solution which I am using now is to use mode: 'no cors' but it doesn't let me to get the response as it is opaque. Another solution I have found is to add more headers:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-Requested-With, X-Auth-Token, Origin, Application");
But it doesn't work as well. Any sugestions? Full error code:
Access to fetch at 'https://aleksanderblaszkiewicz.pl/kiedykolos/add_event.php' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Full php code:
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-Requested-With, X-Auth-Token, Origin, Application");
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";
$connection = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($connection, 'utf8');
if ($connection -> connect_errno) {
echo "Failed to connect to MySQL: " . $connection -> connect_error;
exit();
}
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);
$courseID = $input["courseID"];
$groupID = $input["groupID"];
$date = $input["date"];
$time = $input["time"];
$description = $input["description"];
$typeID = $input["typeID"];
$password = $input["password"];
$sql = "INSERT
INTO `events` (`id`, `date`, `time`, `description`, `course_fk`, `year_group_fk`, `type_fk`)
VALUES (NULL, '$date', '$time', '$description', '$courseID', '$groupID', '$typeID')";
if($password == "Ujebale321!") {
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
echo "Success";
}
else {
echo "Wrong password";
}
mysqli_close($connection);
?>