使用节点js获取授权标头令牌 [英] get authorization header token with node js
本文介绍了使用节点js获取授权标头令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在后端节点js中获得令牌.
I would like to get token in backend node js.
首先,我从jwt获取令牌,并将其存储在localstorage中,但是当我想使用此令牌发送请求时,无法在服务器端获取它.
First, I get the token from jwt and I stored in localstorage,but when i would like to send a request with this token, I can't get it in server side.
客户端:
function list_users(){
url= "http://localhost:8181/users";
var tok = window.localStorage.getItem('token');
if (tok) {
/*
$.ajaxSetup({
headers: {
'x-access-token': tok
}
});
*/
$.ajax({
headers: {'Authorization': tok},
dataType: "application/json; charset=utf-8",
url,
type: 'GET',
dataType: 'json',
success: function (json) {
alert("done");
}
})
}
}
服务器端:
router.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header("Access-Control-Allow-Headers", "Authorization");
console.log(req.headers['authorization']);
...
}
但是
req.headers ['授权']
req.headers['authorization']
打印
未定义"
"undefined"
请提供任何解决方案.
推荐答案
使用如下:
req.header('authorization');
这篇关于使用节点js获取授权标头令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文