vue 使用 proxyTable 解决跨域问题

时间:2023-03-09 14:37:55
vue 使用 proxyTable 解决跨域问题

1、在 main.js 中,在引入 axios:

import axios from 'axios'
Vue.config.productionTip = false Vue.prototype.$axios = axios //将axios挂载在Vue实例原型上 // 设置axios请求的token
axios.defaults.headers.common['token'] = 'f4c902c9ae5a2a9d8f84868ad064e706'
//设置请求头
axios.defaults.headers.post["Content-type"] = "application/json"

2、在 config/index.js 中,找到 dev 下的 proxyTable:

proxyTable: {
'/apis': {
// 测试环境
target: 'http://localhost:8000/', // 接口域名
changeOrigin: true, //是否跨域
pathRewrite: {
'^/apis': '' //需要rewrite重写的,
}
}
}

3、调用的接口页面中,引入:

var api = '/apis/...'
this.$http.get(api).then(function(res) {
// 打印出来
   console.log(...)
},function(err) {
console.log(err)
})