网上好多的解决方法都是
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Authorization,X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method' )
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PATCH, PUT, DELETE')
res.header('Allow', 'GET, POST, PATCH, OPTIONS, PUT, DELETE')
next();
});
你们可以试试,但貌似我用起来不起什么作用
后来又发现cors这个中间件 ,一次成功
- 安装
npm install cors --save-dev
- 引入
const cors = require('cors');
app.use(cors());
这样跨域的问题就解决了