【发布时间】:2018-03-05 13:36:33
【问题描述】:
我有两个基于 express 的服务器...
服务器 1:API 在端口 3010 上运行
服务器 2:UI 在端口 3000 上运行
在 app.js 文件 (autogen) 中的服务器 1 上,我有以下内容...
// Allow requests from the ui
app.use(function (req, res, next) {
// TODO: Make specific
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
但是当我尝试从服务器 2 页面 POST 到该地址时,我在 Chrome 控制台中得到以下信息......
Failed to load http://localhost:3010/search: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我也尝试了res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); 和其他人,但似乎都没有工作
我错过了什么?
【问题讨论】:
-
你也可以看看cors。
-
@pzaenger 谢谢,但看看我的回答,看起来 express autogen 正在某个地方搞砸中间件
-
啊,好吧。我知道了。你可能是对的 ;)