CORS when Axios with Bearer Token token using Codeigniter4 and Vue Vite

Mario tsai
Sep 16, 2023

--

Response to preflight request doesn’t pass access control check ……OMG

Modify File Location: Public/index.php

Edit Code:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
die();
}

reference: https://stackoverflow.com/questions/61925666/cors-issue-in-codeigniter-4-response-to-preflight-request-doesnt-pass-access-c

  • > Change
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");

or

header("Access-Control-Allow-Headers: *");

My 5 hours Spent Result!

--

--

Mario tsai

Major in physical education. Football referee. And high interest in looking the world through the numbers.