1、Node Express 解決請求跨域請求
標簽(空格分隔): 跨域
1是Access-Control-Allow-Origin 允許的域
2是Access-Control-Allow-Headers 允許的header類型
第一項可以直接設為* 表示任意
但是第二項不能這樣寫,在chrome中測試跨域發現報錯,
最終的代碼看起來是這個樣子:
---app.js---
app.all('*', 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();
});
2、node koa2中可以引入koa-cors中間件
---app.js---
const cors = require('koa-cors');
app.use(cors());
以上代碼則可以解決跨域問題