1.安装egg-cors插件
2.plugin中配置插件
exports.cors= {
enable: true,
package: 'egg-cors'
}
3.config.default.js(结合实际文件情况)中配置允许跨域
config.security={
csrf:{
enable:false
},
domainWhiteList:['*']
};
config.cors={
origin: 'http://127.0.0.1:3000',
credentials: true, //允许Cook可以跨域
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
};
4.前端请求时设置withCredentials:true
axios({
method:'post',
url:serverPath.checkLogin,
data:dataProps,
withCredentials:true //表示前后端共享session
}).then(...)
本文介绍了如何在Egg.js框架中配置跨域,包括安装`egg-cors`插件,启用插件配置,以及在`config.default.js`中设定允许的跨域规则,并提示前端请求时需设置`withCredentials`为`true`。
1005

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



