Laravel Cors (Middleware NOT working)

后端 未结 8 1546
孤独总比滥情好
孤独总比滥情好 2020-12-31 12:37

I recently tries enabling CORS in Laravel 5.4 but unfortunately it doesn\'t want to work. I have included the code and the error that it\'s giving me below. Can anyone help

8条回答
  •  春和景丽
    2020-12-31 13:16

    I am using Laravel 6 and up. This url helped me in solving my CORS issue: https://medium.com/@petehouston/allow-cors-in-laravel-2b574c51d0c1

    Use this code instead of code in the url:

    header('Access-Control-Allow-Origin', '*')
                  ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
                  ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type,X-Token-Auth, Authorization');
        }
    }
    

    Also, if you want to use middleware through the entire application then you need to make changes in Kernel.php:

    protected $middleware = [
    \App\Http\Middleware\Cors::class, //add this line to $middleware variable
    ]
    

提交回复
热议问题