我有两个nodejs应用程序,服务器1运行UI代码,服务器2提供后端逻辑和api服务。我试图从服务器1中的UI应用程序调用$ .post jquery方法,在服务器2中发布到API,我得到跨域限制错误,节点Express CORS请求
我在app.js中添加了以下代码, UI服务器(服务器1)中的index.js,但不是徒劳的。
app.js文件
enter code here
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
路线/ index.js文件
router.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
我是新来的NodeJS /快递。
您是否尝试过使用['cors'](https://github.com/troygoode/node-cors)中间件而不是尝试手动操作? – mscdex 2014-11-02 04:17:14