我有一个laravel api应用程序,在localhost(IIS)上一切正常,我可以成功执行GET请求,我在Apache服务器上也有同一个应用程序,我的api突然抛出了CORS enter code here
错误,我正在使用laravel-cors插件
我尝试不允许.htaccess文件上的标头无效。
下面是我的kernel.php的截短版本
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
//'cors' => \App\Http\Middleware\Cors::class,
//'cors' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'cors' => \Barryvdh\Cors\HandleCors::class,
];
答案 0 :(得分:0)
谢谢。我通过将标头直接包含在控制器中的方法/函数中来使其工作。
....
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
有点hacky,但现在必须要做