1.安装跨域插件

C:\Users\SYJ\egg-example>npm i egg-cors --save

2.修改plugin.js文件,配置跨域插件

  // 配置egg-cors插件(先安装插件:npm i egg-cors --save)
  cors: {
    enable: true,
    package: 'egg-cors'
  }

如图所示:

使用Egg.js编写RestfulAPI接口(二)配置跨域

3.修改config.default.js文件,开启跨域并配置允许跨域的请求方法

  // 关闭csrf开启跨域
  config.security = {
    // 关闭csrf
    csrf: {
      enable: false
    }
  };

  // 允许跨域的方法
  config.cors = {
    origin: '*',
    allowMethods: 'GET, PUT, POST, DELETE, PATCH'
  };

如图所示:

使用Egg.js编写RestfulAPI接口(二)配置跨域

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2021-09-04
  • 2021-10-05
  • 2021-10-16
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案