reactjs - 如何在reactjs中将令牌添加到标头
问题描述
我有一个想要使用的网络应用程序api
。我想获取需要令牌的数据。我已经有了令牌 Ii 通过在我的邮递员中运行登录功能获得了令牌)。问题是:我把这个令牌放在哪里?有人说我需要把它放进去,headers
但我找不到在哪里做这个headers
。这是我的代码:
componentDidMount() {
fetch('API_GOES_HERE', {
headers: { Authentication: `Bearer ajsbdjabsjhdbfquhjwbeasbdasdjwbqjbds
}})
.then(res => res.json())
.then(json => {
this.setState({
contact: json,
isLoaded: true,
})
})
.catch(err => console.log(err));
}
没有令牌,api 总是返回 this {"message":"missing or malformed jwt"}
。我已经尝试从邮递员本身获取此数据并且它有效,因为我已经使用我的令牌设置了令牌变量。
ENDPOINT_API
我通过将端点和令牌更改为and来审查端点和令牌API_TOKEN
。谢谢,任何帮助将不胜感激。
解决方案
您需要在请求标头中包含令牌
fetch('API_ENDPOINT', {
headers: { Authentication: `Bearer ${token}`
})
.then(res => res.json())
.then(json => {
this.setState({
contact: json,
isLoaded: true,
})
})
.catch(err => console.log(err));
推荐阅读
- video - .mp3 到视频格式的无损转换
- android - Cordova 未打开相机(用于扫描 qr)
- python - 当其中一个数据跨越多行时,如何使 Treeview 中的一行看起来完整?
- jquery - DropzoneJS上传未开始文件上传
- reactjs - 我如何使用 carto react 绘制形状(点、线)
- c++ - 返回完整的数组 C++?
- android - 如何制作在安装前询问密码并且没有密码不卸载的应用程序模块?
- jquery - 某些月份的自定义视图中的问题 - FullCalendar.io
- powerbi - 如何使用 dax 计算平均体重
- electron - 如何使用 electron-builder 将 2 个 Electron 平台发布到 GitHub?