我們需要安裝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'
}
}
最重要的一點是:在你請求的頁面 不要忘了修改一下請求:
這樣就可以了。。。。。