zoukankan      html  css  js  c++  java
  • 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);
                  })
              });
          },
  • 相关阅读:
    001 Python网络爬虫与信息提取 课程前序
    004 JQuery (010
    Vuex的元素对象
    003 JQuery (009
    002 JQuery (004
    001 JQuery (001
    Vuex简介
    axios实例与模块封装
    axios拦截器
    015 Javascript(152
  • 原文地址:https://www.cnblogs.com/wjbych/p/12892864.html
Copyright © 2011-2022 走看看