【发布时间】:2021-07-27 18:21:09
【问题描述】:
我想用 axios 将数据发送到我的后端。但是 req.headers['Authorization'] 是未定义的。虽然我是用 axios 发送的,但它不在标题中。
axios:
import axios from 'axios';
const entryGroup = async (privatekey, userid) => {
try {
const options = {
headers: {
'Authorization': `Bearer ${userid}`,
'Accept': 'application/json',
},
body: privatekey
};
return axios.post('http://xxxxx:3000/entrygroup', options).then(res => res.data).catch(e => e);
} catch(e) {
return e;
}
};
export default entryGroup;
Nodejs:(所有 cors 都已启用)
const dbf = require('../../functions/database/index');
module.exports = async (req, res) => {
const { body } = req.body;
console.log(req.headers);
};
req.headers 的输出:
{
accept: 'application/json, text/plain, */*',
'content-type': 'application/json;charset=utf-8',
'content-length': '127',
host: 'xxxx:3000',
connection: 'Keep-Alive',
'accept-encoding': 'gzip',
'user-agent': 'okhttp/3.14.9'
}
【问题讨论】:
标签: node.js react-native axios expo