溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue下如何利用resource發(fā)送請(qǐng)求

發(fā)布時(shí)間:2022-10-23 13:03:50 來(lái)源:億速云 閱讀:190 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下vue下如何利用resource發(fā)送請(qǐng)求的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

vue resource發(fā)送請(qǐng)求,代碼如下所示;

<!DOCTYPE html>
<html>
<head>
  <title>vue-resource</title>
  <meta charset="utf-8">
</head>

<body>
  <div id="app">
    <input type="button" value="get請(qǐng)求" @click="getInfo">
    <input type="button" value="post請(qǐng)求" @click="postInfo">
  </div>
</body>
<!-- 基于vue-resource實(shí)現(xiàn)get post請(qǐng)求 也可利用axios第三方包實(shí)現(xiàn)-->
<script src="https://cdn.staticfile.org/vue/2.6.10/vue.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.js"></script>
<script>
  // vue-promise是基于vue的,所以引入CDN庫(kù)時(shí)需要注意順序,應(yīng)先引入vue
  let vm = new Vue({
    el: "#app",
    data:{
    },
    methods:{//vue-resource發(fā)送請(qǐng)求是基于promise的
      getInfo(){
        this.$http.get('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/result/cart.json') 
        .then(res=>{
          console.log(res);
        })
      },
      postInfo(){
        this.$http.post('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/upload',{},{
          emulateJSON:true//設(shè)置這個(gè)參數(shù)相當(dāng)于application/x-www-form-urlencoded,由于手動(dòng)提交請(qǐng)求沒有默認(rèn)表單格式,需要設(shè)置發(fā)送的數(shù)據(jù)格式
        })
        .then(res=>{
          console.log(res.body);
        })
      },
      }
  });
</script>
</html>

知識(shí)點(diǎn)擴(kuò)展;vue-resource請(qǐng)求的幾種方式

安裝import VueResource from 'vue-resource'

Vue.use(VueResource)
get //獲取
getdesignatepl({commit,rootState}){
 // console.log("ppppp")
  let designate_list = `schedule-plan-configuration`;
  Vue.http.get(api.get(designate_list),{headers:rootState.headers}).then(function(response){
    let pup=response.data.data;
   // console.log(response)
  },function(response){
   // console.log("pl")
   var tipsObj = {
      title : "數(shù)據(jù)錯(cuò)誤",
      flag: true,
      type: 4,
      btnGroup:['ook']
    }
    commit('popup',tipsObj,{root:true});
  })
 }
post//新增 
 setAdd({commit,rootState,state,dispatch,context},mss){
 Vue.http.post(api.save('schedule-plan-configuration'),mss,{headers:rootState.headers}).then(function(response){
   dispatch("getdesignatepl")
   commit('popup',titleObj,{root:true});
  },function(response){
   if(response.status == 422){
     commit('popup',tipsObj,{root:true});
  }
  })
 },
 //刪除
 BrDelete(context,obj){
  let br_delete_put_data = `schedule-plan-configuration/${obj.id}`
  Vue.http.delete(api.get(br_delete_put_data),{headers:context.rootState.headers}).then(function(response){ 
   context.dispatch("getdesignatepl");
  },function(response){
    // console.log(response); 
    if(response.status == 401 || response.status == 500){
      var tipsObj = {
        status: response.status,
        flag: true,
        type: 4,
        btnGroup:['ook']
      }
      context.commit('popup',tipsObj,{root:true});
      return;
    }
  })
 },

 // 跟新
 getEntrySales({commit,rootState,dispatch,context},obj){
  // console.log(obj.id)
   Vue.http.put(api.update(`schedule-plan-configuration/${obj.id}`),obj, 
  {headers:rootState.headers}).then(function(response){
    dispatch("getdesignatepl");
    },function(response){
    })
 
 },

以上就是“vue下如何利用resource發(fā)送請(qǐng)求”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI