Laravel cors不在Apache服务器上工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel cors不在Apache服务器上工作相关的知识,希望对你有一定的参考价值。
我有一个laravel api应用程序,所有在localhost(IIS)上工作正常,我可以成功做一个GET请求,我在Apache服务器上有相同的应用程序突然我的api抛出一个CORSenter code here
错误,我正在使用laravel-cors插件
我试过允许.htaccess文件中的标题无效。
下面是我的kernel.php的截断版本
protected $routeMiddleware = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'signed' => IlluminateRoutingMiddlewareValidateSignature::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
//'cors' => AppHttpMiddlewareCors::class,
//'cors' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'cors' => BarryvdhCorsHandleCors::class,
];
答案
谢谢。我通过将标题直接包含在我的控制器中的方法/函数中来实现它。
....
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
有点hacky,但现在必须做
以上是关于Laravel cors不在Apache服务器上工作的主要内容,如果未能解决你的问题,请参考以下文章
Cors 政策问题 Laravel youtube apache