Best practice/how to deal with CORS |
04-12-2024, 06:06 AM
(This post was last modified: 04-12-2024, 06:20 AM by Willen. Edit Reason: Additional context )
Hello!
I have had a project that have been running Codeigniter 3 for a long time and now I'm on a journey to convert it to Codeigniter 4. I have a separate front end and I'm using Codeigniter as an api. When I tested my endpoints in postman it was working fine, then when I tested it in browser I was getting multiple CORS-erros, like: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. I read a bunch of threads on this forum and found one solution in this thread: https://forum.codeigniter.com/showthread.php?tid=80043 . The solution was to make a CORS-Filter and add options-routes for all my endpoints: https://gist.github.com/kenjis/e757d2b41...7e6eaa1254 Cors-filter: Code: <?php Is this the best practice in dealing with cors preflights? Without the options route defined, I get CORS errors. Do I have to add options for all of my endpoints or am I missing something? Code: $routes->group('', ['filter' => 'cors'], static function (RouteCollection $routes): void { |
Welcome Guest, Not a member yet? Register Sign In |