I am currently running a XAMPP server locally at http://localhost on port 80, which serves an API located in the folder kiosk2/api, where api.php acts as the root file. The corresponding .htaccess configuration in kiosk2/api is as follows:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !^src($|/) api.php [L]
Additionally, I have a React application running on localhost:5173 using Vite. When I send a request to an endpoint without specific headers, the operation succeeds:
const response = await fetch(`http://localhost/kiosk2/api/items`);
However, upon attempting to include an authorization token in a different request, I encounter a CORS error:
const responseWithAuth = await fetch(url, {
headers: {
Authorization: "Basic " + btoa(user + ":" + password),
},
});
The following error message appears in the browser:
“Access to fetch at ‘http://localhost/kiosk2/api/login’ from origin ‘http://localhost:5173’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.”
I’m seeking assistance to resolve this issue.