Reputation: 1951
My lumen middleware has the following code
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;
}
I can use postman to send requests and it is getting response too. When I send a post request through vue.js http method, Cross-Origin Request Blocked error is showing.
Upvotes: 0
Views: 2697
Reputation: 1951
Some changes in middleware worked for me
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;
}
Upvotes: 1