Node.js express ajax sessions

I have a problem with express and ajax sessions. My application receives requests through Ajax (POST and GET) and sets a session variable. Although sessions work fine if the request doesn't come from ajax, otherwise they just don't work. For example:

if (typeof req.session.id == 'undefined'){ req.session.id = 1; 

}

 else { req.session.id++; } res.send({session:req.session.id}); 

now when i do localhost: 3000 / all is well, it increases every time. If I do the same from angular via $ http, I keep getting 1. I tried the same in PHP if something is wrong with angular, but everything works fine. Note that the ajax call comes from localhost: 80

My express installation:

 app.use(express.bodyParser()); app.use(express.cookieParser('secret')); app.use(express.cookieSession({secret:'mySecret'})); app.use(express.session({store:new express.session.MemoryStore(),maxAge : new Date(Date.now() + 3600000)})); app.use(app.router) 

EDIT

by running console.log (req); I noticed that when I did not come from AJAX, I get:

  cookies: 

{PHPSESSID: 'jm2p2kn7etmofrp48s8sv4pu47', ZDEDebuggerPresent: 'php', '': 'phtml', user_admin: 'MTUzOnJvb3Q6U2ptaHJpZmllaG8 =', 'connect.sess':' s: j6 + {} + iH44N7YxWZWEP49bTHp1h / gpAElY '}

but when exiting AJAX I get {}

+8
ajax
source share
1 answer

in node.js:

 res.header('Access-Control-Allow-Credentials', 'true'); 

on ajax:

 xhr.withCredentials = true; 
+7
source share

All Articles