我向middleware.json添加了cookie-session
我想在登录时向会话添加id,并在函数addLead
"session": {
"cookie-session": {
"params": {
"name": "id",
"secret": "secret",
"keys": [ "key1", "key2" ],
"httpOnly": "false"
}
}
}
登录:
router.post("/login", function (req, res)
{
req.session.id = req.body.id
req.sessionOptions.id = req.body.id
//...
})
addLead
router.post("/addLead", function (req, res)
{
console.log(req.session.id)
console.log(req.sessionOptions.id)
})
调用addLead
时,我会
未定义
未定义
我用于测试的页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous"></script>
<script>
function log(data)
{
console.log(JSON.stringify(data))
}
$.post("http://localhost:3000/login", { name: "John Smit", id: 15 }, function (data)
{
log(data)
$.post("http://localhost:3000/addLead", {
name: "Anton Berezin", phone: "337225", model: "1234",
mark: "Toyota", yearFrom: "2014", yearTo: "2017"
}, function (data)
{
log(data)
alert(JSON.stringify(data))
})
.fail(function (error)
{
log(error)
})
})
.fail(function(error)
{
alert(JSON.stringify(error))
})
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:0)
“cookie-session”应替换为“express-session”。它会给出加密的cookie,应该以某种方式解密...