在axios的请求中添加以下配置:
axios.post('/api/example', data, {
withCredentials: true,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json;charset=utf-8'
}
}).then(response => {
console.log(response.data)
}).catch(error => {
console.log(error)
})
其中,withCredentials设为true表示允许发送cookie,在请求头中添加X-Requested-With表示该请求为Ajax请求,Content-Type为application/json;charset=utf-8表示请求体为JSON格式。这样,就可以成功发送带有凭据的post请求。