我有兩個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