nodeJS设置cookie后,服务端和客户端都获取不到

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/well2049/article/details/81368217

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

猜你喜欢

转载自blog.csdn.net/well2049/article/details/81368217
今日推荐