vue脚手架的跨域配置_ios

module.exports = {
  devServer: {
open: true,
    proxy: {
      '/getDiscList': {
        target: 'http://localhost:8000/tp5/publica/index/Cook/show',// 要跨域的域名
        changeOrigin: true, // 是否开启跨域
        pathRewrite: {
        '^/api': '/api/' // 在请求的时候 凡是/api开头的地址都可以跨域
        }
      },
    }
  }
};

vue脚手架的跨域配置_nginx_02


vue脚手架的跨域配置_nginx_03

fetch('http://localhost:7000/getDiscList').then(x=>x.json())
.then(res=>{
console.log(res)
})

或者

如果不说明url地址 会默认取服务器的地址localhost什么端口
axios({
url:'/index/index',
baseURL: '/tp5/',
method: 'get'
}).then(x=>{
console.log(x)
})

vue.config.js文件如下
module.exports = {
  devServer: {
open: true,
    proxy: {
      '/tp5': {
        target: 'http://localhost:3000',// 要跨域的域名
        changeOrigin: true, // 是否开启跨域
        pathRewrite: {
        '^/api': '/api/' // 在请求的时候 凡是/api开头的地址都可以跨域
        }
      },
    }
  }
};

vue脚手架的跨域配置_跨域_04


如果不配置vue.config.js 就使用nginx 先把脚手架打包

然后把dist文件放入

vue脚手架的跨域配置_跨域_05


就可以跑起来了

vue脚手架的跨域配置_跨域_06


vue脚手架的跨域配置_nginx_07