Lumen - CORS 缺失允许来源但请求成功
Lumen - CORS missing allow origin but request suceed
我的应用程序有问题Lumen-VueJs
我发出请求,请求状态为 200,我收到了我想要的,但在 'network' 上请求被阻止。 ( 屏幕 )
我的应用程序上有一个类似的 CorsMiddleware,它也被添加到 bootstrap/app。php
<?php
/**
* Location: /app/Http/Middleware
*/
namespace App\Http\Middleware;
use Closure;
class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST,GET,PATCH,PUT,DELETE,OPTIONS',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type,API-KEY'
];
if ($request->isMethod('OPTIONS')) {
return response()->json('', 200, $headers);
}
$response = $next($request);
foreach ($headers as $key => $value) {
$response->header($key, $value);
}
return $response;
}
}
这些是我请求的headers:
我不明白为什么我会出现必须由我的中间件授权的错误
在此先感谢您的帮助!
总结我们在聊天中的讨论,您的浏览器抱怨的问题是正确的。 Access-Control-Allow-Origin
header 未发送。
这是因为您的中间件未被调用,因为您已使用密钥 cors
将中间件添加到 $app->routeMiddleware
。我假设没有名为 cors
的路线。来自 Lumen's GitHub:
These can be global middleware that run before and after each request [$middleware]
into a route or middleware that'll be assigned to some specific routes [$routeMiddleware]
.
由于应该为所有请求调用您的 CORS 中间件,因此您需要将其添加到 $app->middleware
。
我的应用程序有问题Lumen-VueJs
我发出请求,请求状态为 200,我收到了我想要的,但在 'network' 上请求被阻止。 ( 屏幕 )
我的应用程序上有一个类似的 CorsMiddleware,它也被添加到 bootstrap/app。php
<?php
/**
* Location: /app/Http/Middleware
*/
namespace App\Http\Middleware;
use Closure;
class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST,GET,PATCH,PUT,DELETE,OPTIONS',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type,API-KEY'
];
if ($request->isMethod('OPTIONS')) {
return response()->json('', 200, $headers);
}
$response = $next($request);
foreach ($headers as $key => $value) {
$response->header($key, $value);
}
return $response;
}
}
这些是我请求的headers:
我不明白为什么我会出现必须由我的中间件授权的错误
在此先感谢您的帮助!
总结我们在聊天中的讨论,您的浏览器抱怨的问题是正确的。 Access-Control-Allow-Origin
header 未发送。
这是因为您的中间件未被调用,因为您已使用密钥 cors
将中间件添加到 $app->routeMiddleware
。我假设没有名为 cors
的路线。来自 Lumen's GitHub:
These can be global middleware that run before and after each request
[$middleware]
into a route or middleware that'll be assigned to some specific routes[$routeMiddleware]
.
由于应该为所有请求调用您的 CORS 中间件,因此您需要将其添加到 $app->middleware
。