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