【发布时间】:2015-11-12 22:43:22
【问题描述】:
我有一个包含所有文件和信息的主服务器,并且我已经构建了另一个 nodejs 项目来提供图像。当我尝试与它连接时,我看到XMLHttpRequest cannot load http://localhost:2000/addImageURL.The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8754' that is not equal to the supplied origin. Origin 'http://127.0.0.1:8754' is therefore not allowed access.。
要连接,我使用 angular 和 $http。在服务器上,我将我的 8754 端口列入白名单,如下所示。
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8754');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
更新
如果我将所有网址修改为127.0.0.1,则会出现以下错误:XMLHttpRequest cannot load http://127.0.0.1/addImageURL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8754' is therefore not allowed access.
【问题讨论】:
-
你在服务器端使用 Expressjs 吗?
-
这是由于跨域问题而发生的。参考:en.wikipedia.org/wiki/Cross-origin_resource_sharing
-
是的,我正在使用快递。