解決跨域的方法有很多,慢慢總結(jié)间学,現(xiàn)在先介紹最常用的一種
app.all('/*', function(req, res, next) {
//Access-Control-Allow-Origin 允許的域
res.header("Access-Control-Allow-Origin", "*");
//Access-Control-Allow-Methods 允許的請(qǐng)求方法
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
//Access-Control-Allow-Headers 允許的header類型
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
這種的好處在于支持get囱皿,post等各種請(qǐng)求
具體原理可閱讀 阮一峰老師的跨域資源共享 CORS 詳解