egg.js【2-跨域】

    技术2022-07-12  66

    前台vue解决模式请点这里

    egg里解决 安装插件 $ npm i egg-cors --save 配置

    // 在config/plugin.js 一般是这样写配置 如果不生效 module.exports = { // 解决跨域 cors:{ enable: true, package: 'egg-cors', } }; 尝试这种 exports.cors = { enable: true, package: 'egg-cors', }; //然后在 config.default.js里 module.exports = appInfo => { const config = exports = {}; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1593568768041_7880'; //解决跨域 config.security = { domainWhiteList: ['*'], } config.cors = { origin: '*', allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH' }; return { ...config, ...userConfig, }; }; 如果这里不行的话跟上面一样 改为这种方法 exports.cors = { // {string|Function} origin: '*', // {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH' };
    Processed: 0.023, SQL: 9