在Node / Express上启用CORS不起作用
我花了几个小时试图弄清楚这一点…
app.use(function(req, res, next) { 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(); }); app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); });
没有为我工作…我有这个在我的服务器app.js在需求在顶部之后。
还尝试了npm安装cors和app.use(cors())。 也没有工作。
为什么我会遇到这个问题? 我正在使用快速生成器(npm install express-generator)
req.open("GET", "localhost:3001/quote");
本来应该…
req.open("GET", "http://localhost:3001/quote");
标题不是问题。