本文介绍了如何在Laravel中启用CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Laravel 5.8中-我一直收到这个CORS问题
I am in Laravel 5.8 - I kept getting this CORS issue
我尝试过
php artisan make:middleware Cors
添加这些代码
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->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’);
}
}
重新启动本地Apache 2 sudo apachectl -k restart
restart my local Apache 2 sudo apachectl -k restart
打开app/Http/Kernel.php
-添加了这1行
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\AdminMiddleware::class,
'dev' => \App\Http\Middleware\DevMiddleware::class,
'cors' => \App\Http\Middleware\Cors::class, <-----
];
刷新站点,转到控制台,仍然看到相同的CORS问题
refresh the site, go to console, still see the same CORS issue
人们将如何进行进一步的调试?
How would one go about and debug this further?
推荐答案
尝试 laravel-cors 软件包,可让您使用Laravel中间件配置发送跨域资源共享标头.
Try laravel-cors package that allows you to send Cross-Origin Resource Sharing headers with Laravel middleware configuration.
这篇关于如何在Laravel中启用CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!