title: CORS issue in codeigniter 4: Response to preflight request doesn't pass access control check
body: <p>I'm making an api with Codeigniter 4 for a react application. Everything works fine in postman but when I make requests with axios (also tried fetch), it gets CORS error</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at '<a href="http://localhost:8080/testpost" rel="nofollow noreferrer">http://localhost:8080/testpost</a>' from origin '<a href="http://localhost:3000" rel="nofollow noreferrer">http://localhost:3000</a>' 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.</p>
  </blockquote>
  
  <p>I tried adding headers to base controller:</p>
  
  <pre><code>header('Access-Control-Allow-Origin: *');
  header("Access-Control-Allow-Methods: POST,GET, OPTIONS");
  header("Access-Control-Allow-Headers: *");
  </code></pre>
  
  <p>Now it works fine with requests without JSON body, but when I send json body same error occurs.</p>
  
  <pre><code>axios.post("http://localhost:8080/testpost", { data: "test" })
      .then((response) =&gt; {
          console.log(response);
      })
      .catch((err) =&gt; {
          console.log("error!!!");
      });
  </code></pre>
  
  <pre><code>// Routes.php
  $routes-&gt;post('/testpost', 'Home::testPost');
  </code></pre>
  
  <pre><code>// Home Controller
  public function testPost()
  {
      return $this-&gt;response-&gt;setJSON('test response');
  }
  </code></pre>
  
  <p>Thanks for your help</p>