axios中Post請求的兩種區別


一、axios   post請求ashx

一般處理程序(ashx)的好處就是容易上手,直接按住寫就行了,但是需要對提供的參數處理一下,不然后端接收不到

         var params={
              user:this.yhm,
              pwd:this.$md5(this.pwd).toUpperCase(),
          };

 

//Post方法的封裝
      axiosPost:function(url,params){
          return new Promise((resolve, reject) => {
                  this.$axios({
                  url: url,
                  method: 'post',
                  data: params,
                  transformRequest: [function(data) {
                      let ret = ''
                      for(let it in data) {
                          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                      }
                      console.log(ret)
                      return ret
                  }],
                  headers: {
                      'Content-Type':'application/json'
                  }
              })
              .then(res=>{
                  resolve(res.data);
              })
          });
      },

 

二、axios POST請求webapi

這里的webapi我是使用的 .net core3.1 webapi,有控制器、路由等很是好用,post時參數是json格式的,所以在vue中就不需要再對參數進行轉換了

 //Post方法的封裝
      axiosPost:function(url,params){
          return new Promise((resolve, reject) => {
                  this.$axios({
                  url: url,
                  method: 'post',
                  data: params,
                  // transformRequest: [function(data) {
                  //     let ret = ''
                  //     for(let it in data) {
                  //         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                  //     }
                  //     console.log(ret)
                  //     return ret
                  // }],
                  headers: {
                      'Content-Type':'application/json'
                  }
              })
              .then(res=>{
                  resolve(res.data);
              })
          });
      },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM