1.定义请求头
const headers = {
'USER_INFO': { "userId": 001, "userName": "admin" }
};
2.将header对象中的每个属性转换为字符串
const headerToString= Object.keys(headers).reduce((acc, key) => {
acc[key] = typeof headers[key] === 'string' ? headers[key] :
JSON.stringify(headers[key]);
return acc;
}, {});
3.发送请求带上header
axios({
method: 'post',
url: '/xxx/xxx',
data:data,
headers: headerToString
}).then((res) => {
if (res.data == 200) {
//...
}
}).catch(()=>{
//...
});