egg.js后台解决跨域问题

    技术2022-07-31  77

    我们需要安装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' } }

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

    这样就可以了。。。。。

     

    Processed: 0.009, SQL: 9