背景:
前端服务运行在localhost:8080
后端服务运行在127.0.0.1:8000
使用axios请求前已设置
axios.defaults.withCredentials = true
document.cookie = 'username=hello'
axios请求代码:
axios.post('/api/post', data, {
baseURL: 'http://127.0.0.1:8000',
})
后端已设置允许跨域
$response = $next($request);
$origin = $request->server('HTTP_ORIGIN') ? $request->server('HTTP_ORIGIN') : '';
$allow_origin = [
'http://localhost:8080',
];
if (in_array($origin, $allow_origin)) {
$response->header('Access-Control-Allow-Origin', $origin);
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
$response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
$response->header('Access-Control-Allow-Credentials', 'true');
}
return $response;
但是在请求的时候,请求头里没有该cookie,是什么原因?
这些代码看不出问题吧