一個網站正在做一個XMLHttpRequest我節點的應用程序,但他們得到以下錯誤:CORS在我的節點的應用程序不能正常工作
XMLHttpRequest cannot load http://[MY IP]/start. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.[THEIR WEBSITE].com' is therefore not allowed access. The response had HTTP status code 504.
這是他們的XMLHttpRequest請求:
var xhr = typeof XMLHttpRequest === "undefined" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
xhr.open("POST", 'http://[MY IP]/myapp', true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
這是節點應用程序中的代碼(...代表不相關的代碼):
...
var cors = require('cors')
...
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET,POST');
next();
});
app.post('/myapp', function (req, res) {
var data = req.body;
console.log(data);
res.status(200);
});
你知道爲什麼這不是加工?
感謝您的建議。
爲什麼你不利用CORS庫? –
我是? VAR CORS =需要( 'CORS') –