在route的过滤器里统一获取令牌信息
router.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization,token, X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Content-Type", "application/json;charset=utf-8");
var Authorization = req.headers['Authorization'];//undefined
var token = req.headers['token']//undefined
}
next()
});
以下是客户端请求
$(function () {
$.ajax({
type: "get",
url: "http://localhost:8081/account/list",
dataType: "json",
ContentType: "application/json",
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("token", "fsddfsd");
XMLHttpRequest.setRequestHeader("Authorization", "fdfdds");
}
});
});
同样的客户端,用.NET试过能获取到自定义的信息,然而用node.js则是undefined,为何??