nodeJS设置cookie后获取不到的几种情况分析
- 第一,是否使用了nginx?
如果,真的只是做个端口代理,是不会受影响的。 - 第二,是否使用了axios?
axios默认是不允许设置cookie的,所以如果用了,需要你单独启动一下全局设置axios.defaults.withCredentials = true;//让ajax携带cookie
在入口的页面设置即可全局使用。 - 第三,node后台启动时是否设置了允许携带
cookie
?
这个简单,重新设置一下即可
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://192.168.199.246:3001");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header('Access-Control-Allow-Credentials', 'true');
res.header("X-Powered-By",' 3.2.1')
next();
});
当然主要就是这句
:res.header('Access-Control-Allow-Credentials', 'true');