gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:13 24 4
gpt4 key购买 nike

跨域请求时未设置 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();
}});

客户端使用Angualarjs,cors配置为

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

最佳答案

express

app.use(require('cors')({
origin: function (origin, callback) {
callback(null, origin);
},
credentials: true
}));

有 Angular

$httpProvider.defaults.headers.common['X-Requested-With'] ='XMLHttpRequest';
$httpProvider.defaults.withCredentials = true;

关于angularjs - cookie 未设置跨域 - AngularJS 和 NodeJS/Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29250894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com