在我的 React 应用程序中,我正在使用 axios执行 REST API 请求。
但无法随请求发送Authorization header 。
这是我的代码:
tokenPayload() {
let config = {
headers: {
'Authorization': 'Bearer ' + validToken()
}
}
Axios.post(
'http://localhost:8000/api/v1/get_token_payloads',
config
)
.then( ( response ) => {
console.log( response )
} )
.catch()
}
这里的 validToken()
方法将简单地从浏览器存储中返回 token 。
所有请求都有 500 错误响应,表明
The token could not be parsed from the request
来自后端。
如何在每个请求中发送授权 header ?您会推荐任何其他带有 React 的模块吗?
最佳答案
const config = {
headers: { Authorization: `Bearer ${token}` }
};
const bodyParameters = {
key: "value"
};
Axios.post(
'http://localhost:8000/api/v1/get_token_payloads',
bodyParameters,
config
).then(console.log).catch(console.log);
第一个参数是 URL。
第二个是将随您的请求一起发送的 JSON 正文。
第三个参数是标题(除其他外)。这也是 JSON。
关于reactjs - 使用 axios 发送不记名 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40988238/