gpt4 book ai didi

javascript - 如何在 axios.all() 中附加标题

转载 作者:行者123 更新时间:2023-12-04 09:06:33 25 4
gpt4 key购买 nike

有没有办法在 axios.all 方法中附加 header ,或者我们是否必须单独将 header 附加到每个请求。

axios.all([
axios.delete('http://localhost:5000/requests/'+{id}),
axios.post('http://localhost:5000/records/'+{id, response}),
],{
headers: {Authorization: localStorage.getItem('auth-token')}
}).then(res => console.log(res.data))
.catch(err => console.log(err));

最佳答案

创建 axios实例并为所有请求使用相同的实例(推荐)

const axiosInstance = axios.create({
headers: {'X-Custom-Header': 'foobar'}
});
样本:

const axiosInstance = axios.create({
headers: {
Authorization: localStorage.getItem('auth-token')
}
});

axios.all([
axiosInstance.delete("http://localhost:5000/requests/" + {id}),
axiosInstance.post("http://localhost:5000/records/" + {id, response})
]).then((res) => {
console.log("Response: ", res);
}).catch((error) => {
console.log("Error: ", error);
})

关于javascript - 如何在 axios.all() 中附加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63430249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com