【问题标题】:x-access-token not added to POST requestx-access-token 未添加到 POST 请求中
【发布时间】:2020-12-19 16:20:36
【问题描述】:

我在请求或创建资源时使用来自 Redux 操作的 Axios 调用来处理用户身份验证/授权。我已经成功完成了令牌的所有创建/存储/签名。当用户对资源发出 GET 请求时,标头会像这样传递:

        return axios.get('applicants/', { headers: authHeader() }).then(result => {
        const applicants = []
        result.data.forEach(item => {
            applicants.push(item)
        })

而且效果很好。标头包含应有的“x-access-token”。当我做这样的 POST 时不起作用:

        return axios.post('applicants/create', { headers: authHeader() })
        .then(result => {
            dispatch(_addApplicants(result.data))
        })

“x-access-token”不包含在标头中。相同的函数为两个路由提供了令牌,所以我确信它与不同的 HTTP 动词有关。有人有什么想法吗?

GET 上的标题:

{
  host: 'localhost:3000',
  connection: 'keep-alive',
  accept: 'application/json, text/plain, */*',
  'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmNGJkMTRhMzcwOGNiNTViMDhmOGM1ZCIsImlhdCI6MTU5ODgwOTI0OCwiZXhwIjoxNTk4ODk1NjQ4fQ.7k4xi3kPo-aKyOf_5GNVv3pjO_WsIZwg8Ja1MgwfJCg',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
  origin: 'http://localhost:8080',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  referer: 'http://localhost:8080/',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-US,en;q=0.9',
  'if-none-match': 'W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w"'
}

POST 上的标题:

{
  host: 'localhost:3000',
  connection: 'keep-alive',
  'content-length': '204',
  accept: 'application/json, text/plain, */*',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
  'content-type': 'application/json;charset=UTF-8',
  origin: 'http://localhost:8080',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  referer: 'http://localhost:8080/',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-US,en;q=0.9'
}

【问题讨论】:

    标签: express http axios jwt jwt-auth


    【解决方案1】:

    您将标题作为第二个参数传递给axios.post,但实际上它应该是第三个参数。第二个参数是data

        axios.post(
            "/applicants/create",
            {}, // add empty object or null here as second argument
            {
              headers: authHeader()
            }
          })
    

    【讨论】:

    • 非常感谢您。现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多