【发布时间】:2021-07-20 21:09:18
【问题描述】:
我正在尝试访问 http://localhost:8080/api/v1/postulant,但我无法在标头中设置访问令牌。我在 Postman 上做了同样的请求,它可以工作,所以这是 Axios 请求中的问题,我做错了什么?
let headers = {
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTYxOTUyNDE4NywiZXhwIjoxNjIwMTI4OTg3fQ.hSXARB-y7rClswYZ380HV5RW77qjYNt5FzW2NfDd8Vw",
"Content-Type": "application/json",
};
axios.get("http://localhost:8080/api/v1/postulant", { headers });
在 api 我有这个代码来控制令牌:
function Auth(req, res, next) {
console.log(req.headers);
if (!req.headers["authorization"])
res.status(400).json({ message: "token invalid" });
}
它为我的控制台日志 req.header 提供了: command prompt result
根据我的邮递员请求,我得到了这个结果:enter image description here
所以我没有找到我在 Axios 请求中提供的授权
【问题讨论】:
-
Content-Type在两个请求中都不同——“application/x-www-form-urlencoded”与“application/json” -
和 postmann 一起工作
标签: reactjs express axios request cors