I use express-jwt and create my token via jQuery and save it in my localStorage with:
$.ajax({
url: "http://localhost:8080/login",
type: 'POST',
data: formData,
error : function(err) {
console.log('Error!', err)
},
success: function(data) {
console.log('Success!')
localStorage.setItem('token', data.id_token);
}
});
I have a protected route in my backend like:
app.get('/upload',jwt({secret: config.secret}), function(req, res) {
res.sendFile(path.join(__dirname + '/upload.html'));
});
How can I send the token from localStorage with the request header?
You can set the headers in a $.ajax request:
$.ajax({
url: "http://localhost:8080/login",
type: 'GET',
// Fetch the stored token from localStorage and set in the header
headers: {"Authorization": localStorage.getItem('token')}
});
这篇关于如何使用来自 jQuery 的 AJAX 请求发送令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Math.random() 在密码学上是安全的吗?Is Math.random() cryptographically secure?(Math.random() 在密码学上是安全的吗?)
在javascript中保护随机数?Secure random numbers in javascript?(在javascript中保护随机数?)
使用过期令牌发出同时 API 请求时如何避免多个令How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用过期令牌发出同时 API 请求时如何
JWT 未解码“JWT malformed";- 节点角度JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解码“JWT malformed;- 节点角度)
如何使没有到期时间的 JWT 令牌无效How to invalidate a JWT token with no expiry time(如何使没有到期时间的 JWT 令牌无效)
img src 链接中的授权标头Authorization header in img src link(img src 链接中的授权标头)