POST中流明的CORS问题

时间:2017-05-09 13:30:51

标签: php laravel vue.js lumen

我的流明中间件具有以下代码

$time1 = strtotime($firsttime);
$time2 = strtotime($secondtime);
$interval  = abs($time2 - $time1);
$diffinminutes   = round($interval / 60);

我可以使用邮递员发送请求,它也会得到回应。当我通过vue.js http方法发送post请求时,显示Cross-Origin Request Blocked错误。

1 个答案:

答案 0 :(得分:1)

中间件的一些变化对我有用

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;
}