Vue关于axios跨域问题的解决

1、在项目根目录config文件夹下找到index.js

2、修改index.js中proxyTable{}内容:

proxyTable: {
      '/api': {
        target: 'http://192.168.199.190:9998',//设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://192.168.199.190:9998/login',直接写‘/api/login’即可
        }
      }
    }

保存,npm start命令重启就可以了

3、在组件中的使用

this.$axios.post('/api/login', this.$qs.stringify(this.loginData))
	.then((result) => {
		console.log(result)
	}).catch((error) => {
		console.log(error); //箭头函数"=>"使this指向vue
        });
发布了94 篇原创文章 · 获赞 42 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_29483485/article/details/93893942