在routes/api.php最下面加上这行代码就可以解决跨域访问问题,(如前后端分离请求api、请求只出现一个options请求方式进程):
Route::options('/{all}', function(Request $request) { $origin = $request->header('ORIGIN', '*'); header("Access-Control-Allow-Origin: $origin"); header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE'); header('Access-Control-Allow-Headers: Origin, Access-Control-Request-Headers, SERVER_NAME, Access-Control-Allow-Headers,Authorization, cache-control, token, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');})->where(['all' => '([a-zA-Z0-9-]|/)+']);