Vue網(wǎng)絡(luò)請(qǐng)求與跨域配置

一、Fetch配置 https://blog.csdn.net/qq_42492055/article/details/82593692

1、在config配置文件中的index.js中的跨域區(qū)域中配置proxyTable

proxyTable: {
      '/apis':{    //名字自己定義,以后接口的域名用他來(lái)替換
        target: 'http://cc.lzjoy.com/', //接口域名
        changeOrigin: true, //是否跨域
        pathRewrite: {
          '^/apis': ''  //需要rewrite重寫(xiě)
        }
      }
    },

2、發(fā)起網(wǎng)絡(luò)請(qǐng)求

    //1、get請(qǐng)求
    created(){
      console.log("請(qǐng)求數(shù)據(jù)")
      fetch("/apis?urlparam=pad/index/getindexdata", {
        method: "get",
        headers:{
          "Content-Type": "application/json",
        },
      }).then(result=>{
        return result.json()
      }).then(result=>{
        console.log(result)

      }).catch(err=>{
        alert(err)
      })
    }
    
    //2、post請(qǐng)求
    created(){
      console.log("請(qǐng)求數(shù)據(jù)")
      fetch("/apis?urlparam=pad/index/getindexdata", {
        method: "post",
        headers:{
          "Content-Type": "application/json",
        },
        //這里要放要上傳的參數(shù)
        body: JSON.stringify({name: "abc", phone: "123456"})
      }).then(result=>{
        return result.json()
      }).then(result=>{
        console.log(result)
      }).catch(err=>{
        alert(err)
      })
    }

二、axios配置

https://blog.csdn.net/qq_42492055/article/details/82593692

0、安裝axios

npm install axios

1、在config配置文件中的index.js中的跨域區(qū)域中配置proxyTable

    proxyTable: {
      '/apis':{    //名字自己定義,以后接口的域名用他來(lái)替換
        target: 'http://cc.lzjoy.com/', //接口域名
        changeOrigin: true, //是否跨域
        pathRewrite: {
          '^/apis': ''  //需要rewrite重寫(xiě)
        }
      }
    },

2、在程序入口 main.js文件中引入并配置axios

import Axios from 'axios'

// 設(shè)置axios
Vue.prototype.$axios = Axios
// Axios.defaults.baseURL = '/apis'
Axios.defaults.headers.post['Content-Type'] = 'application/json'

3、發(fā)起請(qǐng)求

    //1、get請(qǐng)求
    created(){
        console.log("頁(yè)面加載")
        this.$axios.get("/apis?urlparam=pad/index/getindexdata")
            .then(res=>{
              console.log(res)
              alert(res)
            })
            .catch(err=>{
              console.log(err)
            })
      }
      //2、 post請(qǐng)求 name、phone是要發(fā)送的參數(shù)
    created(){
        this.$axios.post('/apis?urlparam=pad/index/getindexdata', {
              name: 'Fred',
              phone: '123456'
            })
            .then(function (response) {
              console.log(response);
              alert(response)
            })
            .catch(function (error) {
              console.log(error);
            });
    }
?著作權(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)容

  • 今天我是來(lái)臺(tái)灣學(xué)習(xí)參觀的第11天,我從老師的身上學(xué)到了很多,首先就是不要給自己貼標(biāo)簽,如果你覺(jué)得自己是這樣的,那...
    日光傾城_625c閱讀 389評(píng)論 0 0
  • 感恩今天早上6:30就被時(shí)間管理群的伙伴語(yǔ)音電話叫醒了,測(cè)試云之家使用狀態(tài),為明天的周會(huì)做好準(zhǔn)備,未雨綢繆,謝謝!...
    泰來(lái)媽媽閱讀 220評(píng)論 0 0

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