My problem concerns the client side when I am trying to get the session through Node with express.
For example with this snippet https://gist.github.com/visionmedia/1491756 I have always "viewed 0 times" with my ajax request but with my browser it's all right and the variable incrementation is ok.
I have tried with PHP and all works. With browser and Ajax requests.
It is the same problem as this post : Session cookies do not change using ajax and nodejs server
I use this code in order to get some JSON data and to get the nodeJS Session :
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = function () {
alert(xhr.responseText);
if (xhr.readyState != 4 || xhr.status != 200) return;
var response = {};
response = xhr.responseText;
console.log(response);
return response;
}
xhr.open(type, url);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(params);
For NodeJS, I use the following code :
// In my app.js
// To enable CORS
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Credentials', 'true');
next();
};
//...
// The session "visitCount"
app.get('/', function(req, res, next) {
req.session.visitCount = req.session.visitCount ? req.session.visitCount + 1 : 1;
res.send('You have visited this page ' + req.session.visitCount + ' times');
});
Thanks for your help !