在 Axios 中,可以使用 headers 属性来设置请求头部信息。以下是一个示例代码:
axios.get('/api/data', {
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'Authorization': 'Bearer your_token_here'
}
})
.then(response => {
// 处理响应数据
})
.catch(error => {
// 处理错误
});
在这个示例中,我们向 /api/data 发送一个 GET 请求,并在请求头部设置了三个自定义的头部信息:X-Requested-With、Content-Type 和 Authorization。
你可以根据自己的需求修改这些头部信息的内容。