因此,我設置了一個應用程序以允許跨域請求。在這篇文章中使用了各種方法How to enable cross-origin resource sharing (CORS) in the express.js framework on node.jsCORS問題與節點
但是,我得到一個錯誤,我似乎無法理清。這是我正在使用的(不是我嘗試了3種方法 - 他們都給出了相同的錯誤)。
app.use(function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
})
.options('*', function(req, res, next){
res.end();
});
這仍然給我一個錯誤:
XMLHttpRequest cannot load http://localhost:3000/blah. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
如果我註釋掉上面代碼中,我得到預期不允許錯誤,似乎表明該請求是否正確由代碼處理。我還重新輸入了內容類型的請求,以確保我沒有粘貼奇怪的字符。任何線索?