Vue跨域问题解决

项目根目录下创建vue.config.js

module.exports = {
  devServer: {
    proxy: {
      //配置跨域
      '/api': {
        //这里是真实的后台接口
        target: 'https://localhost:5001/',
        //允许跨域
        changeOrigin: true,
        //重写路径
        pathRewrite: {
          /* 
            浏览器中看到请求的地址为:http://localhost:8080/api/region
            实际上访问的地址是:https://localhost:5001/api/region
            如果请求中不存在/api则设置为'^/api': '',
           */
          '^/api': '/api',
        },
      },
    },
  },
}
设置为'^/api': ''所有的请求中如果不是/api/开始的请求也需要写上会自动替换成后边的值

配置完成后需要重新启动vue项目

原文地址:https://www.cnblogs.com/liessay/p/14193111.html