在 Laravel Lumen 5.1 中,我在 CorsMiddleware 中使用它:
public function handle($request, \Closure $next)
{
$response = $next($request);
$response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE');
$response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
$response->header('Access-Control-Allow-Origin', '*');
return $response;
}
但是在 5.2 中它不再工作了。
如何在响应中返回适当的 header ?
最佳答案
浏览器使用 OPTIONS
请求来检查端点上是否有任何 CORS header 。这意味着,您需要首先处理这些飞行前 OPTIONS 请求,为它们提供正确的 header 。
在 Lumen/Laravel 中,您还需要将 CORS 中间件添加到与 CORS 资源相同的路由,但使用 OPTIONS 方法请求:
$app->options(
'your/cors/uri',
[
'middleware' => 'cors', function()
function() { return response('', 200); }
]
);
有同样的问题,所以我希望这对你有帮助:)
关于Laravel Lumen 5.2 Cors 中间件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412707/