首页 > 解决方案 > 使用 axios 请求 node.js api 问题

问题描述

您好,我尝试访问 http://localhost:8080/api/v1/postulant 但我无法在标头中设置访问令牌。

let headers = {
        'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTYxOTUyNDE4NywiZXhwIjoxNjIwMTI4OTg3fQ.hSXARB-y7rClswYZ380HV5RW77qjYNt5FzW2NfDd8Vw',
        'Content-Type': 'application/json',
    }
    axios.get("http://localhost:8080/api/v1/postulant", { headers })

在我使用的 api 上:

app.get('/api/v1/postulant',Auth,function (req,res){
console.log(req.headers)
db.query('SELECT * FROM postulant_view', (err, rows) => {
    if (err) { console.log(err); }
    else {
        console.log(rows)
        let result = rows;
        res.json(result);
    }
})

并且我的请求的标题给出了:[命令提示符结果

我尝试过:

app.get('/test',Auth,function (req,res){
console.log(req.headers)
db.query('SELECT * FROM postulant_view', (err, rows) => {
    if (err) { console.log(err); }
    else {
        console.log(rows)
        let result = rows;
        res.json(result);
    }
})

并打电话给

let headers = {
        'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTYxOTUyNDE4NywiZXhwIjoxNjIwMTI4OTg3fQ.hSXARB-y7rClswYZ380HV5RW77qjYNt5FzW2NfDd8Vw',
        'Content-Type': 'application/json',
    }
    axios.get("http://localhost:8080/test", { headers })

第二个我收到了:请求 2

所以它有效有人解释为什么第二个有效而第一个无效。

标签: node.jsaxiosrequest-headers

解决方案


推荐阅读