与NodeJS会话和AJAX请求不一致

时间:2013-08-03 20:59:15

标签: javascript node.js session express

当我尝试通过Node with express时,我的问题与客户端有关。

例如,使用此代码段https://gist.github.com/visionmedia/1491756我总是使用我的ajax请求“查看0次”,但是我的浏览器没问题,变量增量就可以了。

我尝试过使用PHP和所有工作。使用浏览器和Ajax请求。

与此帖相同的问题:Session cookies do not change using ajax and nodejs server

修改

我使用此代码来获取一些JSON数据并获取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);

对于NodeJS,我使用以下代码:

// 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');
});

感谢您的帮助!

0 个答案:

没有答案