vue項(xiàng)目中axios的使用及跨域問(wèn)題解決

1.npm install axios 安裝axios,在main.js中引入并將axios改寫(xiě)為Vue的屬性

import axios from 'axios'
Vue.prototype.$axios = axios
Vue.prototype.HOME='https://xxxx.xxx.com'  // 發(fā)布正式或者測(cè)試版本不需要跨域,直接調(diào)用url即可
// Vue.prototype.HOME = '/api' // this.HOME 本地調(diào)試

2.找到config/index.js中的proxyTable配置代理來(lái)實(shí)現(xiàn)跨域請(qǐng)求

proxyTable:{
    '/api':{
      target:"https://xxxx.xxx.com", // 接口域名
      changeOrigin:true,
      pathRewrite:{
        '^/api':""
      }
    }
 }

3.在頁(yè)面中this.axios調(diào)用即可

// post
this.$axios.post('/api/login',requestBody).then(res=>{
    console.log(res);
},err=>{
    console.log(err);
})
// get
let url = this.HOME + '/api/login'
let data = this.$axios.get(url, {
  params: {
    name:name,  // get的參數(shù)寫(xiě)在params中
    pwd:pwd
  }
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容