cookie 未设置跨域 - AngularJS 和 NodeJS/Express

riy*_* tk 3 cookies cross-domain node.js angularjs

跨域请求时未设置 cookie。我的服务器在 localhost:8000 中运行,客户端在 localhost:9000 中运行。服务器nodejs/express上的cors设置是

app.use(function(req, res, next) {
console.log(req.method);
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control, Authorisation");
if (req.method === 'OPTIONS') {
    return res.send(200);
} else {
    return next();
}});
Run Code Online (Sandbox Code Playgroud)

客户端使用Angualarjs,cors配置为

SelappsAdmin.config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
Run Code Online (Sandbox Code Playgroud)

小智 5

快车上

app.use(require('cors')({
  origin: function (origin, callback) {
    callback(null, origin);
  },
  credentials: true
}));
Run Code Online (Sandbox Code Playgroud)

在有角度的

$httpProvider.defaults.headers.common['X-Requested-With'] ='XMLHttpRequest';
$httpProvider.defaults.withCredentials = true;
Run Code Online (Sandbox Code Playgroud)