如何从jQuery发送带有AJAX请求的令牌 [英] How to send a token with an AJAX request from jQuery
本文介绍了如何从jQuery发送带有AJAX请求的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用express-jwt并通过jQuery创建令牌,并使用以下命令将其保存在localStorage中:
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'));
});
如何从带有请求标头的localStorage发送令牌?
推荐答案
您可以在 $ .ajax 请求:
$.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请求的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文