1、安裝插件
????npm install egg-cors --save
2排作、配置插件
????config/plugin.js中
????exports.cors = {???
????????enable: true,
????????package: 'egg-cors'
????}
3、配置安全域名
????config/config.default.js中
????exports.security = {
????????csrf: false,
????????domainWhiteList: ['http://localhost:8080'],
????};
4、配置允許跨域
????exports.cors = {
????????//不能寫*
????????origin: 'http://localhost:8080',
? ? ????allowMethods: 'GET,PUT,POST,DELETE',
? ? ????// 該屬性允許cookie跨域
????????credentials: true
????};
5、在前端框架中攜帶credentials參數(shù)
????例如vue:
????????this.$http.get(api, {credentials: true}).then((res)=>{
????????}