Jeżeli ktoś mi wyjaśni dlaczego tak się dzieje, dostanie Nobla.
Dwa skrypty i dwa fetche:
fetch_cars_data.php
<?php
session_start();
include "db_conn.php";
header("Access-Control-Allow-Origin: {$_SESSION['request_origin']}");
//header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Credentials: true");
$stmt = $pdo->query("SELECT * FROM cars");
$cars_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo(json_encode($cars_data));
?>
delete_rental.php
<?php
session_start();
include "db_conn.php";
header("Access-Control-Allow-Origin: {$_SESSION['request_origin']}");
//header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type, Accept");
$rental_id = json_decode(file_get_contents("php://input"), true)["rentalId"];
$stmt = $pdo->prepare("DELETE FROM scheduled_rentals WHERE rental_id = :rental_id");
$stmt->execute(["rental_id" => $rental_id]);
if($stmt->rowCount() === 1)
echo(json_encode(["deleted" => true]));
else
echo(json_encode(["deleted" => false]));
?>
Fetche:
fetch(`${API_URL}fetch_cars_data.php`, {
method: "GET",
credentials: "include"
})
.then((response) => response.json())
.then((data) => setCardModels(data));
};
fetch(`${API_URL}delete_rental.php`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ rentalId: id })
});
Pierwszy fetch działa, sesja jest wykrywana i gitara - fetch do każdego innego skryptu również działa.
Drugi fetch nie działa, zmienna $_SESSION jest pusta.
Ktoś potrafi to wyjaśnić?