想要使用axios在标头中发送JWT令牌和数据,但出现以下错误:在将标头发送到客户端后无法设置标头



//用于用户登录验证的生成器函数函数*addExpense(数据({

console.log(data)
const ItemName = data.name;
const ItemAmount = data.amount;
const ItemCategory = data.category;
const token = localStorage.getItem('token')
console.log(localStorage.getItem('token'))
try {
const payload1 = yield call(axios.post,'/routes/api/expenses/add-expense',{
method:'POST',
body:JSON.stringify({ItemName,ItemAmount,ItemCategory})
},{
headers:{
'auth-token':token,
'Content-type': 'application/json'
}           
})

@Nikhil如果你在使用redux传奇?如果你正在使用Axios,那么你不需要使用Axios——你可以通过fetch或直接调用轻松完成。这是一个参考资料,我希望它能帮助你。

export function* apicall(data){
const token = localStorage.getItem('token')
const header ={
Accept: 'application/json',
'Content-type': 'application/json'
'auth-token':token,
}
const options = {
method,
mode: 'cors',
header: header
body: JSON.stringify({ItemName,ItemAmount,ItemCategory}),
};
try{
const payload1 = yield call(URL,options);
yield[
yield put(action,payload1),
notification and etc facality
]
}catch(error){
yield put(action,error), 
}
}

或者您也可以使用所需的header选项为API调用创建单独的utilfor请求。

fetch方法也是如此。为了给出一个正确的答案,我需要你的代码部分,特别是API调用部分的更多细节,这样我就可以给你一个更正确的答案。

最新更新