Welcome Guest, Not a member yet? Register   Sign In
Error with cors :(
#1

hello good,

I am developing a frontend in angular but when I want to connect to my api in codeigniter 4 it throws me a cors error

Codeigniter port 8080
Angular port 4200


My filter corsFilter
Code:
public function before(RequestInterface $request, $arguments = null)
    {
        if (array_key_exists('HTTP_ORIGIN', $_SERVER)) {
            $origin = $_SERVER['HTTP_ORIGIN'];
        } else if (array_key_exists('HTTP_REFERER', $_SERVER)) {
            $origin = $_SERVER['HTTP_REFERER'];
        } else {
            $origin = $_SERVER['REMOTE_ADDR'];
        }
        $allowed_domains = array(
            'http://localhost:4200'
        );


        if (in_array($origin, $allowed_domains)) {
            header('Access-Control-Allow-Origin: ' . $origin);
        }
        header('Access-Control-Allow-Origin: ' . '*');

        header("Access-Control-Allow-Headers: Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token");
        header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, PATCH, OPTIONS");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Max-Age: 3600");
        header('content-type: application/json; charset=utf-8');
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            header("HTTP/1.1 200 OK CORS");
            die();
        }

    }
Reply
#2

(This post was last modified: 08-09-2023, 02:36 AM by kenjis.)

Do not use
Code:
header('Access-Control-Allow-Origin: ' . '*')
Reply
#3

(08-09-2023, 02:15 AM)kenjis Wrote: Do not use
Code:
header('Access-Control-Allow-Origin: ' . '*')
Hello,
I put it to test, I was reviewing and testing, if I don't send the token in the header it works fine but if I send my authorization the cors fails locally

In production it works fine with the token since it is in the same url
Reply




Theme © iAndrew 2016 - Forum software by © MyBB