post请求中无法取到session里的数据
发布于 8 年前 作者 HuKaihe 5593 次浏览 来自 问答

我使用express-session来处理sesseion,但很奇怪,在任何请求中设置的session属性,在get请求中都能获得,但是在post请求中都获得不了,请问是为什么啊? index.hbs <h1>{{title}}</h1> <p>Welcome to {{title}}</p> <form action="/a" method=“post”> <input type=“submit” value=“提交”> </form> app.js(部分) var session = require(‘express-session’); app.use(session({ secret: foo’, key: ‘bar’, cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days resave: false, saveUninitialized: true })); index.js var express = require(‘express’); var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
	console.log('come into get');
	req.session.a = 'dd';
	console.log(req.session);
	res.render('index', {title: 'Express'});
});

router.post('/a', function (req, res, next) {
	console.log('come into post');
	console.log(req.session);
	req.session.user = {username: 'hukaihe'};
	res.redirect('/');
});

module.exports = router;

post里的session始终输出: Session { cookie: { path: ‘/’, _expires: Sun Mar 05 2017 15:35:23 GMT+0800 (中国标准时间), originalMaxAge: 2592000000, httpOnly: true } }

3 回复

为啥你的console.log(req.session)写在req.session.user = {username: ‘hukaihe’}之前

@L-TMC req.session.user = {username: ‘hukaihe’}没有什么太大作用,主要是上面的req.session.a也取不到,不知道怎么回事

你是如何存储sessionid的?

回到顶部