vue跨域和react跨域配置

263 阅读1分钟

vue跨域和react跨域配置

1.vue跨域

3737cf635ab466d4408ccdb966f5c98.jpg

db1476eb6a9247cee5ad33213617838.jpg

附源码:
proxyTable: {
      "/api": {  // 请求前缀
        target: "https://api.xxxxxx.com", //  请求转发给谁
        changeOrigin: true, //  允许跨域
        pathRewrite: {
          "^/api": "" // 重写请求前缀
        }
      }
    },

2.react跨域

03a19d06e625a54ca75e89a6ed2a6a1.jpg f9d01cc060dbd096ba1326e5781d410.jpg

附源码:
const proxy = require('http-proxy-middleware')

module.exports = function(app) {
  app.use(
    proxy('/api', {
      //  /api  前缀的请求,就会触发概代理
      target: 'https://api.xxxxxx.com', //  请求转发给谁
      changeOrigin: true, //  控制服务器收到的请求头中Host的值 默认值 false
      pathRewrite: {
        '^/api': '', //  重写请求前缀
      },
    })
  )
}

当然配置项很多,上面的配置项是最简单的,也是必须的,如果还需要别的配置项,主要看项目需求,然后结合项目进行配置即可。