解决前后端的跨域问题:Access to XMLHttpRequest at '**' from origin '**' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

发布时间 2024-01-11 17:26:55作者: .Tik

报错信息:

Access to XMLHttpRequest at 'http://localhost:8182/cooperationRequest/getList' from origin 'http://localhost:3004' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 解决方法:

在项目根目录下新建vue.config.js文件

module.exports = {

    devServer: {
      proxy: {
        '/api': {
          target: 'http://127.0.0.1:8182/api/',// 后端接口
          changeOrigin: true, // 是否跨域
          pathRewrite: {
            '/api': ''
          }
        }
      }
    }
  }