nodejs设置不上cookie

图片描述

想在注册成功之后设置cookie 但是设置不上 也没报错

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

阅读 2k
1 个回答

ajax 默认是不会带 cookie,您需要手动设置以下 withCredentials

xhr.withCrendentials = true;

为了保障该属性生效,服务器必须显式地返回Access-Control-Allow-Credentials这个头信息:

Access-Control-Allow-Credentials: true

跨域问题。您先用*试试。

res.header("Access-Control-Allow-Origin", "*"); 

我这边是基于express写的接口:

var app = express();

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*"); //*表示允许的域名地址,本地则为'http://localhost'
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, X-Powered-By, Accept,X-Requested-With");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
推荐问题