Php 以内腔发行CORS,用于邮寄
我的lumen中间件具有以下代码Php 以内腔发行CORS,用于邮寄,php,laravel,vue.js,lumen,Php,Laravel,Vue.js,Lumen,我的lumen中间件具有以下代码 public function handle($request, Closure $next) { //Intercepts OPTIONS requests if($request->isMethod('OPTIONS')) { $response = response('', 200); } else { // Pass the request to the next middleware
public function handle($request, Closure $next)
{
//Intercepts OPTIONS requests
if($request->isMethod('OPTIONS')) {
$response = response('', 200);
} else {
// Pass the request to the next middleware
$response = $next($request);
}
// Adds headers to the response
$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', '*');
// Sends it
return $response;
}
我可以使用邮递员发送请求,它也得到了响应。当我通过vue.js http方法发送post请求时,会显示跨源请求阻塞错误 中间件中的一些更改对我很有用
public function handle($request, Closure $next)
{
$allowedDomains = array("http://localhost:8080");
$origin = $request->server('HTTP_ORIGIN');
if(in_array($origin, $allowedDomains)){
//Intercepts OPTIONS requests
if($request->isMethod('OPTIONS')) {
$response = response('', 200);
} else {
// Pass the request to the next middleware
$response = $next($request);
}
// Adds headers to the response
$response->header('Access-Control-Allow-Origin', $origin);
$response->header('Access-Control-Allow-Methods', 'OPTIONS, HEAD, GET, POST, PUT, PATCH, DELETE');
$response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
}
// Sends it
return $response;
}
你用铬吗?Chrome本身就在阻止跨源请求。这种情况在Chrome和firefox中都会发生。我更改了apache2.conf,它似乎不起作用。但为什么在php中执行时失败?请尝试向我们提供此包以进行处理