我使用laravel6和angular 8 我想通过API将角度项目中的POST请求发送到服务器 但是在使用OPTION请求方法之后,在chrome浏览器中给出了错误:
访问“ http://localhost:8000/api/user/register”处的XMLHttpRequest 来自来源“ http://localhost:4200”的信息已被CORS政策阻止: 对预检请求的响应未通过访问控制检查:否 请求中存在“ access-control-allow-origin”标头 资源。
我在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');
}
然后在内核中定义中间件:app / Http / kernel.php
'cors' => \App\Http\Middleware\Cors::class,
并定义路线:
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');
});
});
但是我再次遇到相同的错误 请帮我解决