可以通过在Axios的配置中设置withCredentials
属性为true
来启用浏览器的凭据传递。例如:
axios.get('/api/user', {
withCredentials: true
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
在以上示例中,Axios将在请求中发送凭据,以便服务器可以验证用户身份。注意,这需要服务器响应头中设置Access-Control-Allow-Credentials: true
,以允许浏览器在跨域请求时发送凭据。