Rafie aydin ihsan
Rafie aydin ihsan

Reputation: 31

Laravel 8 from origin 'http://localhost:8000' has been blocked by CORS policy

I have problem in a Axios Laravel crossite, which returns this erorr:

Access to XMLHttpRequest at 'http://localhost/moddle/moodle/login/index.php' from origin 'http://localhost: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.

I have done follow like laravel-cors and answer on stackoverflow, none of them worked.

This is my Axios:

let axiosConfig = {
                    headers: {
                        "Access-Control-Allow-Origin": "*",
                        'Content-Type': 'application/json;',
                        "Content-Type": "application/x-www-form-urlencoded",
                    }
                };
                axios.post('http://localhost/moddle/moodle/login/index.php', response.data, {
                    crossOrigin: null,
                }, axiosConfig).then(
                    response => {
                        console.log(true);
                        window.location.href = "http://localhost/moddle/moodle/my/";
                    }).catch(erorr => {

                });

In my cors.php

'paths' => ['api/*', 'sanctum/csrf-cookie'],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => false,

In my middleware

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        // 
        // \App\Http\Middleware\Cors::class,
    ];

Upvotes: 3

Views: 12499

Answers (3)

Serge AHOUANSINOU
Serge AHOUANSINOU

Reputation: 149

php artisan config:cache

Works for me.

Upvotes: 0

Anup
Anup

Reputation: 179

You can add into TrustHosts.php Middleware without doing anything extra. read the complete solution here: https://stackoverflow.com/a/70361284/2612926

Upvotes: 1

Divya Bhaloidiya
Divya Bhaloidiya

Reputation: 5064

Please do the following to resolve the issue.

php artisan config:cache

Upvotes: 2

Related Questions