Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

eggjs跨域解决

  • 进入项目目录,打开命令行

    • 安装cors插件

      cnpm install egg-cors -S
      
  • 打开config/plugin.js,找到如下内容

    module.exports = {
        //里面添加下面三行
        cors: {
            enable:true,
            package:'egg-cors'
        }
    }
    
  • 打开config/config.default.js,找到return字段

    • return前面添加下面内容
      config.security = {
          csrf:{
              enable: false,
              ignoreJSON: true
          },
          domainWhiteList:[http://localhost:8080]
      };
      
      config.cors = {
          origin: '*',
          allowMethod: 'GET,HEAD,PUT,POST,DELETE,PATCH'
      }