我们需要安装egg-cors插件:
npm/cnpm install egg-cors --save
yarn add egg-cors --save
添加相关配置
在egg项目的config/plugins.js
module.exports = {
cors: {
enable: true,
package: 'egg-cors'
}
}

在egg项目的 config/config.default.js
module.exports = {
const config = exports = {}
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: [ '*' ], // 白名单
};
config.cors = {
origin: '*', // 如果不写origin则会按照白名单中的域名允许跨域 * 代表允许所有的域名进行跨域请求
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
}
}

最重要的一点是:在你请求的页面 不要忘了修改一下请求:

这样就可以了。。。。。
本文介绍如何在Egg.js项目中安装并配置egg-cors插件以实现跨域请求。通过npm或yarn添加egg-cors,并在config/plugins.js及config/config.default.js中进行相关设置,同时调整请求页面以确保跨域功能正常运行。
2861

被折叠的 条评论
为什么被折叠?



