Node.js+Express跨域问题解决
package.json
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5", [重点!!!]
"express": "^4.17.1",
"mysql": "^2.18.1",
"node-uuid": "^1.4.8"
},
app.js
const cors = require('cors');
app.use(cors())
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(); // 执行下一个路由
})
还没有评论,来说两句吧...