var cors = require(‘koa2-cors’); // 跨域 const allowOrigins = [ “http://localhost:8080” // 需要跨域的端口,与本服务器端口不一样,请注意。 ]; app.use(cors({ origin: function(ctx) { if (allowOrigins.includes(ctx.header.origin)) { return ctx.header.origin; } return false; }, exposeHeaders: [‘WWW-Authenticate’, ‘Server-Authorization’], maxAge: 5, credentials: true, withCredentials:true, allowMethods: [‘GET’, ‘POST’, ‘DELETE’], allowHeaders: [‘Content-Type’, ‘Authorization’, ‘Accept’], }));
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119836163