我在 Laravel 5.8 - 我一直收到这个 CORS 问题
我试过了
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
打开 app/Http/Kernel.php
添加了这一行
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 问题
如何进一步调试呢?
原文由 code-8 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试 laravel-cors 包,它允许您使用 Laravel 中间件配置发送跨源资源共享标头。