获取具有节点js的授权标头令牌
我想在后端节点js中获取令牌。
首先,我从jwt获取令牌并将其存储在localstorage中,但是当我想使用此令牌发送请求时,我无法在服务器端获取它。
客户端:
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 [ ‘授权’]
打印
“不确定”
请解决任何问题。
尝试这样的事情:
..... ......... ............ $.ajax({ headers: {'Authorization': 'Bearer '+tok}, .......... .......