如何使用React在响应头中获取x-auth值?

问题描述 投票:1回答:1

刚遇到一个新问题,我的令牌被响应头从服务器传递为'x-auth'

enter image description here

似乎反应不能让我通过使用response.headers.x-auth访问此值,

如果我做console.log(response.headers),它确实显示

{content-type: "application/json; charset=utf-8", x-auth: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1Y…jE0fQ.vDdRlqqgmdhJgy9g20QnbZ6HVSkJv_ZXdeOdeAHwBhw"}

但是如果我使用console.log(response.headers.x-auth),它会得到'undefined',我的假设是它将x-auth视为x-auth而不是属性。

我该如何解决这个问题?谢谢!

javascript json reactjs axios
1个回答
1
投票

您不能通过点表示法使用破折号访问对象的属性(因为破折号将被解释为减号),但您可以使用括号表示法来执行此操作:

response.headers['x-auth']
© www.soinside.com 2019 - 2024. All rights reserved.