在routes/api.php最下面加上這行代碼就可以解決跨域訪問問題,(如前后端分離請(qǐng)求api、請(qǐng)求只出現(xiàn)一個(gè)options請(qǐng)求方式進(jìn)程):
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-]|/)+']);
