I use laravel6 and angular 8 I want send POST request in angular project to server by an API but after OPTION request method and give an error in chrome browser:
Access to XMLHttpRequest at 'http://localhost:8000/api/user/register' 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.
I created a middleware in App\Http\Middleware:
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', 'http://localhost:4200')
->header('Access-Control-Allow-Methods', 'POST, OPTIONS')
->header('Access-Control-Allow-Credentials', 'true')
->header('Access-Control-Max-Age', '10000')
->header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With');
}
then define middleware in kernel : app/Http/kernel.php
'cors' => \App\Http\Middleware\Cors::class,
and define route:
Route::group(['prefix' => 'user','middleware' => 'cors'], function () {
Route::post('login', 'Api\AuthController@login');
Route::post('register', 'Api\AuthController@register');
Route::group(['middleware' => 'auth:api'], function(){
Route::post('getUser', 'Api\AuthController@getUser');
});
});
But again I get the same Error Please help me solve it