[英]CORS issue for Ionic + express
我创建了一个应用程序,该应用程序从不同域(例如domain_name)上的mongo / node + express中访问/获取数据。
get函数的代码是:
var request = $http({
method: 'GET',
url: 'https://domain_name.users.io/categories/list',
withCredentials: true /* to get the Cookie value generated at server-side */
});
在快速方面,已添加以下代码,以避免CORS问题:
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");
res.header("Access-Control-Allow-Credentials", "true");
对于上述情况,我收到以下错误:
XMLHttpRequest cannot load https://domain_name.users.io/data/list. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8100' is therefore not allowed access.
我已经检查了API“ https://domain_name.users.io/data/list ”,它没有任何问题,因为我可以看到数据(在浏览器上单击时)。
有人可以帮我一样吗
此外*
太宽容了,会破坏凭据的使用。 因此,请使用https://domain_name.users.io/data/list
而不是使用*
。
您不能喜欢*
因为这是安全性的一部分,并且如果您想允许凭据,则您的Access-Control-Allow-Origin不得使用*。
有关更多信息,请在这里阅读。
必须设置标题:
var request = $http({
method: 'GET',
url: 'https://domain_name.users.io/categories/list',
headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
withCredentials: true /* to get the Cookie value generated at server-side */
});
=============在节点侧===============
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization, Access-Control-Allow-Origin, Access-Control-Allow-Headers');
next();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.