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);
              })
          });
      },

 

posted @ 2020-05-15 09:05  wjbych  阅读(1281)  评论(0编辑  收藏  举报