在開本地服務器與其他人協作開發測試接口時,前端發送請求時遇到Access-Control-Allow-Origin的錯誤,此時只需要在後臺加上
app.use('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1');
if(req.method=="OPTIONS") res.send(200);/*讓options請求快速返回*/
else next();
});
即可解決