Vue2配置axios跨域
这个系列主要记录自己实习期间的踩坑过程,不完全准确,只能说这个方法确实解决了我自己的问题,欢迎交流,但不喜勿喷:
main.js
import Axios from 'axios'
Vue.config.productionTip = false
Vue.prototype.$axios = Axios
Vue.prototype.HOME = '/api'
config/index.js (proxyTable:中是新加入的内容)
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
//注意,测试阶段使用,上线后和后端是在同一个服务器的,不需要跨域
proxyTable: { //axios跨域处理
'/api':{ //此处并非一定和url一致
target: 'http://127.0.0.1:8083',
changeOrigin: true,//允许跨域
pathRewrite:{
'^/api':''
}
}
},
在需要获取后台数据的组件中
mounted: function () {
this.$axios.get(this.HOME + '/park-manage/select-all')
.then((res) => {
console.log(res.data)
this.tableData = res.data.extra
})
.catch(err => {
console.log(err)
})
}
我在后端设置传输的json数据格式
。