I’m receiving the following, though I’ve configured CORS in my middleware:
XMLHttpRequest at ‘http://localhost:8000/api/check-admin’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure(IlluminateHttpRequest): (IlluminateHttpResponse|IlluminateHttpRedirectResponse) $next
* @return IlluminateHttpResponse|IlluminateHttpRedirectResponse
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
return $next($request);
$response->headers->set('Access-Control-Allow-Origin', 'http://localhost:4200');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
if ($request->getMethod() === "OPTIONS") {
return response('', 200)->withHeaders([
'Access-Control-Allow-Origin' => 'http://localhost:4200',
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Request-With',
'Access-Control-Allow-Credentials' => 'true',
]);
}
return $response;
}
}
'paths' => ['api/*','sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
but still the above error pops up. any suggestions?
I tried event add it manually to the index.php file. but still didn’t work. is there any way to resolve this?
Savith Kandegedara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1