Axios/React-JsonWebTokenError:jwt必须被提供
创始人
2024-09-28 14:30:34
0

在发出请求时,需要在请求头中添加jwt令牌。具体实现方法如下:

1.在后端生成jwt令牌,并返回给前端; 2.前端在接收到令牌后,将其保存到localStorage或sessionStorage中; 3.在每次请求时,从localStorage或sessionStorage中获取令牌,并添加到请求头中。

示例代码:

1.后端生成jwt令牌并返回给前端:

const jwt = require('jsonwebtoken'); const user = { username: 'admin' }; const token = jwt.sign(user, 'secret', { expiresIn: '1h' }); res.json({ token });

2.前端接收到令牌后,保存到localStorage中:

axios.post('/login', { username, password }) .then(res => { localStorage.setItem('token', res.data.token); });

3.在每次请求时,获取令牌并添加到请求头中:

const token = localStorage.getItem('token'); axios.get('/api/users', { headers: { 'Authorization': 'Bearer ' + token } }) .then(res => { console.log(res.data); }) .catch(error => { console.log(error); });

相关内容

热门资讯

透视计算"wepok... 透视计算"wepoker怎么看牌型"详细辅助分享教程(透视辅助下载技巧);科技安装教程;136704...
透视挂!红龙poker透视工具... 透视挂!红龙poker透视工具,约局吧德州可以透视吗,规律教程(有挂详情)1、游戏颠覆性的策略玩法,...
透视中牌率!智星菠萝可以辅助吗... 透视中牌率!智星菠萝可以辅助吗,pokermaster脚本,必赢方法(有挂解密)1、智星菠萝可以辅助...
透视总结"德普之星透... 1、透视总结"德普之星透视辅助软件激活码"详细辅助揭秘教程(养号规律方法);详细教程。2、德普之星透...
透视免费!hhpoker透视脚... 透视免费!hhpoker透视脚本视频,We poker辅助器下载,2025新版教程(有挂辅助);进入...
透视脚本"hhpok... 1、透视脚本"hhpoker是真的还是假的"详细辅助安装教程(数据分析教程)。2、hhpoker是真...
透视中牌率!wepoekr底牌... 透视中牌率!wepoekr底牌透视,uupoker有透视吗,新2025版(有挂方法)1、在wepoe...
透视黑科技"hhpo... 透视黑科技"hhpoker透视脚本下载"详细辅助曝光教程(究竟有透视透视)1、点击下载安装,微扑克w...
透视线上!hhpoker透视脚... 透视线上!hhpoker透视脚本安卓,德普之星辅助软件,第三方教程(有挂辅助)1、实时hhpoker...
透视教程"wepok... 此外,数据分析德州(wepoker辅助工具)辅助神器app还具备辅助透视行为开挂功能,通过对客户we...