示例代码:
// axios拦截器
import axios from 'axios'
const token = 'xxx' // 从登录接口获取token
axios.interceptors.request.use(
(config) => {
config.headers.Authorization = `Bearer ${token}` // 添加授权信息
return config
},
(error) => {
Promise.reject(error)
}
)
// 发送PUT请求
axios.put('/api/update', {data}, {
headers: {
'Content-Type': 'application/json',
},
}).then((res) => {
console.log(res.data)
}).catch((error) => {
console.log(error.response.data)
})
注意:需要将授权信息添加到请求头中,具体格式根据后端API接口要求而定。