【问题标题】:Nodejs external server error connectionNodejs外部服务器错误连接
【发布时间】: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.

【问题讨论】:

标签: angularjs node.js http


【解决方案1】:

试试这个

res.setHeader('Access-Control-Allow-Origin', *);

如果有效,则表示您为交叉来源'http://127.0.0.1:8754' 提供的网址是错误的。

【讨论】:

  • 当我将 * 放入 '*' 时,客户端出现错误: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.
  • 好的,我发现我的代码失败部分:) 小姐从客户端给端口内部帖子。
猜你喜欢
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 2020-10-13
  • 2013-12-11
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多