vue proxy 跨域代理

1.config/index.js 修改 proxyTable

    proxyTable: {
      '/api': {
        target: 'http://shuige.wicp.vip/',  //目标接口域名
        changeOrigin: true,  //是否跨域
        pathRewrite: {
          '^/api': '/'   //重写接口 后台接口指向不统一  所以指向所有/
        }
      },
      cssSourceMap: false
    },

2. 请求得时候 接口地址加上前缀

import axios from 'axios'
export default {
  created() {
    // 1. 代理成功
    // http://shuige.wicp.vip/users/proxy  转换成 http://localhost:8080/api/users/proxy 
    axios.get('/api/users/proxy').then(res => { // /api 不能少
      console.log(res)
    })
  }
}

 

posted @ 2020-04-08 14:31  曾经的水哥  阅读(8090)  评论(0编辑  收藏  举报