express前后端分离session的使用

express前后端分离session的使用
1、后端app.js中增加

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "前端地址如:http://localhost:8080");
    //允许跨域后session的存取
    res.header('Access-Control-Allow-Credentials', "true");
    next();
});

2、前端,使用ajax()而不是getJSON()

    $.getJSON("");//无法加xhrFields:{withCredentials:true}
    $.ajax({
       usr: "",
       type: "",
       xhrFields:{
           withCredentials:true
       },
       success:function () {

       },
        error: function () {

        }
    });
posted @ 2019-06-20 08:58  浪羽  阅读(639)  评论(0编辑  收藏  举报