我将 cookie-session 添加到 middleware.json 我想在登录时将 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>