Vue前台解决跨域问题

2020-03-13  本文已影响0人  废弃的种子

前台页面的访问

##### 当访问的路径以student开头时,访问的url会替换成http://localhost/delstu
  delstu(id){
                   fetch("/student/delstu.php",{
                       method:"POST",
                       body:id,
                       headers:{
                           "Content-Type": "application/x-www-form-urlencoded" //设置为表单提交
                       }
                   }).then(res=>res.json()
                   ).then(res=>{
                       this.$message.success(res.msg);
                       setTimeout(function () {
                           history.go();
                       },3000)
                   })
            },

在项目根目录下新建 vue.config.js创建如下:

module.exports = {
    devServer: {
        proxy: {
            '/student': {
                target: "http://localhost",
                ws: true,
                changeOrigin: true,
                  pathRewrite:{
                      "^/student":""
                }
            },
        },
    }
};

配置参考

const path = require("path");

function resolve (dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  chainWebpack: (config) => {
    config.resolve.alias
      .set("assets", resolve("src/assets"))
      .set("components", resolve("src/components"))
      .set("@assets", resolve("src/iview/assets"))
      .set("@components", resolve("src/iview/components"));
  },
  productionSourceMap: false,
  lintOnSave: true,
  publicPath: "/customerservice_mobile/vue-version/dist/",
  devServer: {
    overlay: {
      warnings: false,
      errors: false
    },
    proxy: {
      "/api": {
        target: "http://customservice.vipgz1.idcfengye.com",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": ""
        }
      },
      "/apa": {
        target: "http://localhost:8082",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/apa": ""
        }
      },
      "/test": {
        target: "http://customservice.test.bestpay.net",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/test": ""
        }
      },
      "/dev": {
        target: "http://localhost:8082",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/dev": ""
        }
      },
    }
  }
};

上一篇下一篇

猜你喜欢

热点阅读