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