在 Axios PUT 请求中设置身份验证头,可以使用axios.defaults.headers或者在请求配置中设置headers字段。
以下是使用axios.defaults.headers设置身份验证头的示例代码:
import axios from 'axios';
// 设置身份验证头
axios.defaults.headers.common['Authorization'] = 'Bearer your_token';
// 发送 PUT 请求
axios.put('https://example.com/api/endpoint', { data })
.then(response => {
// 请求成功处理
console.log(response.data);
})
.catch(error => {
// 请求错误处理
console.error(error);
});
以下是在请求配置中设置headers字段的示例代码:
import axios from 'axios';
// 发送 PUT 请求
axios.put('https://example.com/api/endpoint', { data }, {
headers: {
'Authorization': 'Bearer your_token'
}
})
.then(response => {
// 请求成功处理
console.log(response.data);
})
.catch(error => {
// 请求错误处理
console.error(error);
});
在上述示例代码中,将your_token替换为实际的身份验证令牌。