Javascript 为什么我能';无法在Express中获取身份验证标头?
我在Javascript 为什么我能';无法在Express中获取身份验证标头?,javascript,node.js,express,axios,Javascript,Node.js,Express,Axios,我在axios的帮助下从前端发送POST请求,如下所示: const headers = { headers: { 'Authorization': `Bearer ${localStorage.token}`, } } API.post('validate', headers ) .then(res => { })
axios
的帮助下从前端发送POST
请求,如下所示:
const headers = {
headers: {
'Authorization': `Bearer ${localStorage.token}`,
}
}
API.post('validate', headers )
.then(res => {
})
.catch(error => {
})
在browser console.log中,我看到headers
对象作为有效负载添加到请求中,而不是添加到头中
router.post('/', errorHandler(async (req, res, next) => {
console.log(req.method)
console.log(req.headers)
let bearerHeader = req.headers['Authorization']
console.log(bearerHeader)
})
这是我的控制台。登录Express:
POST
{ host: 'localhost:5000',
connection: 'keep-alive',
'content-length': '244',
accept: 'application/json, text/plain, */*',
origin: 'http://localhost:3000',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'content-type': 'application/json;charset=UTF-8',
referer: 'http://localhost:3000/signin',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,uk;q=0.6' }
undefined
我还使用cors库来处理cors(前端和后端的不同本地主机)
我做错了什么?如何在axios的帮助下将auth添加到标题中?POST的第二个参数是有效负载。第三个参数是标题。试着这样做:
API.post('validate',{},headers)
post的第二个参数是有效负载。第三个参数是标题。试着这样做:
API.post('validate',{},headers)
您是否尝试将其作为axios默认对象上的授权头?您是否尝试将其作为axios默认对象上的授权标头?不客气,但我甚至不知道Axios是什么:)这只是标准的Node.js语法。惊人的标准化:)Axios是一个基于承诺的库,用于发送请求(GET-POST等)。不客气,但我甚至不知道Axios是什么:)这只是标准的Node.js语法。惊人的标准化:)Axios是一个基于承诺的库,用于发送请求(GET POST等)。
import cors from 'cors'