cors跨域的問題經(jīng)常會困擾著開發(fā)人員,nodejs做服務(wù)端也是如此投放。
解決方法有2種:
1、代碼控制
//設(shè)置跨域訪問
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
2绒北、借助cors這個模塊來解決這個問題黎侈。
npm install cors --save
var cors = require('cors');
app.use(cors({
origin:['http://localhost:8083'],
methods:['GET','POST'],
alloweHeaders:['Conten-Type', 'Authorization']
}));