使用laravel5.3做接口,前端跨域访问。
当前端发起请求时,浏览器会先发起options请求,就是cors跨域的问题,先是请求一个test.php,请求是成功的
然后请求laravel5.3的接口,options请求没有返回设定的header
Route::any('/user', function (Request $request) {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: content-type,authorization");
return [$request->user()];
})->middleware('auth:api');

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
laravel里面尽可能不要调用各种请求和响应相关的php函数,而是使用他本身封装的request和response对象