vue+axios 配置反向代理解决跨域问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuoganliwanjin/article/details/84671233

首先配置config下的index.js

dev: {
    // Paths
    assetsSubDirectory: 'static',
    autoOpenBrowser: false,
    assetsPublicPath: '/',
    proxyTable: { // 在这里配置如下代码
        '/api': {
            target:'http:xxxxxxxxxxx', // 你请求的第三方接口
            changeOrigin:true, // 在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
            pathRewrite:{  // 路径重写,
                '^/api': ''  // 这里的value要设置为空否则返回404
            }
        }
    }
    
  }

我得请求代码

var url = '/api/xxxxxxxx';
  	this.$axios.get(url,{
    			pageNum: 1,
			pageSize: 10
		})
		.then(function (response) {
			console.log(response);
		})
		.catch(function (error) {
			console.log(error);
		});

猜你喜欢

转载自blog.csdn.net/zhuoganliwanjin/article/details/84671233