问题重现:连接后端接口时一直报跨域问题,而他本地连没问题(这好像是句废话)

报错::8080/#/login:1 Access to XMLHttpRequest at ‘http://192.168.1.105:9999/admin/tenant/list’ from origin ‘http://192.168.1.103:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

解决思路:使用proxy代理解决,注意,代理只能解决本地,上线到服务器时一定要改成线上ip

解决过程:本次项目使用的Vue Cli3 ,所以在vue.config中配置一下代理。(Cli2则需要在config下的index配置)

devServer: {
    disableHostCheck: true,
    port: 8080,
    proxy: {
      '/api': {
        target: url,//代理的地址
        changeOrigin: true,//是否跨域
        ws: false, // 需要websocket 开启
        pathRewrite: {
          '^/api': '/' //和上方api名称一致
          //然后axios.defaults.baseURL = '/api'; 就可以了
        }
      }
    }
  }
Logo

前往低代码交流专区

更多推荐