jeudi 4 juin 2020

No 'Access-Control-Allow-Origin' header is present on the requested resource LARAVEL, AXIOS, REACT

I am doing an Axios POST request and getting the below issue:

Access to XMLHttpRequest at 'https:../api/../1' from origin 'http://127.0.0.1:8000' 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. app.js:63976 Error: Network Error at createError (app.js:633) at XMLHttpRequest.handleError (app.js:176)

However I know for this CORS issue i need to manually allow the config. I used the following code creating a middleware Cors:

        header("Access-Control-Allow-Origin: *");
        //ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS,PUT,DELETE',
            'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin, Authorization',
        ];
        if ($request->getMethod() == "OPTIONS"){
            //The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return response()->json('OK',200,$headers);
        }
        $response = $next($request);
        foreach ($headers as $key => $value) {
            $response->header($key, $value);
        }
        return $response;

But the problem didn't resolve. I also used https://github.com/fruitcake/laravel-cors. But getting same issue. I have tried all possible solution but no outcome. I need help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire