这个问题通常是由于跨域请求带来的。为了使axios支持带凭证的请求,需要在服务器端设置Access-Control-Allow-Credentials头。如果使用的是laravel框架,可以在中间件中设置该头信息:
public function handle($request, Closure $next)
{
$response = $next($request);
$response
->header('Access-Control-Allow-Origin', $request->header('Origin'))
->header('Access-Control-Allow-Credentials', 'true');
return $response;
}
另外,需要在axios的请求中设置withCredentials为true,以便将跨域请求中的cookie发送到服务器端:
axios.get('http://example.com/api/user', {
withCredentials: true
})
.then(response => {
console.log(response.data);
});
这样就可以在axios的响应中正确地接收和存储cookie了。