Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jack2jm/2dce285dd056a93fa94d1bb3812c2205 to your computer and use it in GitHub Desktop.

Select an option

Save jack2jm/2dce285dd056a93fa94d1bb3812c2205 to your computer and use it in GitHub Desktop.
1. Create a cors file
Run this artisan command:
php artisan make:middleware Cors
2. Now open Cors.php from *App\Http\Middleware* folder and replace handle() function with this code:
public function handle($request, Closure $next)
{
//return $next($request);
header("Access-Control-Allow-Origin: *");
// ALLOW OPTIONS METHOD
$headers = [
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
];
if($request->getMethod() == "OPTIONS") {
// The client-side application can set only headers allowed in Access-Control-Allow-Headers
return Response::make('OK', 200, $headers);
}
$response = $next($request);
foreach($headers as $key => $value)
$response->header($key, $value);
return $response;
}
3. Lastly, open Kernel.php from App\Http folder add the below line to the $routeMiddleware array:
protected $routeMiddleware = [
//other routes
'opencors' => \App\Http\Middleware\Cors::class,
];
4. Now add middleware to that route. use below line.
Route::get('/eva-api/get-reports', ['middleware' => 'opencors', 'as' => 'getReport', 'uses' => 'Boilerplate\ChatbotsController@getChatbotsReportByParams']);
Reference link - https://www.codegrepper.com/code-examples/whatever/has+been+blocked+by+CORS+policy%3A+Response+to+preflight+request+doesn%27t+pass+access+control+check%3A+It+does+not+have+HTTP+ok+status.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment