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) })

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM