vue中的axios请求

1、get请求

// get
this.$axios.get('请求路径')
.then(function (response) {
    console.log(response); // 成功
  })
.catch(function (error) {
    console.log(error);  // 失败
  });

2、post请求

// post
this.$axios.post('请求路径', {
    firstName: 'da',
    lastName: 'xiong'
  })
  .then(function (response) {
    console.log(response); / /成功
  })
  .catch(function (error) {
    console.log(error);  // 失败
  });

3.或者在内部定义请求方式

this.$myAxios({
    method: "post",//指定请求方式
    url: "请求路径",
    data: {
       id:"123"
    }
  })
  .then(function(response){
    //接口成功返回结果执行
  })
  .catch(function(error){
  //请求失败或者接口返回失败
  })

4.实际应用的一个案例

this.$axios
        .post(
          this.domain + "/interfaceFsm/rest/job/queryDllConstant",
          {
            sysName: this.sysName,
            key: "PRODUCT_ID_AUTO"
          },
          {
            transformRequest: [
              function(data) {
                var str = "";
                for (var key in data) {
                  str +=
                    encodeURIComponent(key) +
                    "=" +
                    encodeURIComponent(data[key]) +
                    "&";
                }
                return str;
              }
            ]
          }
        )
        .then(response => {
          if ("0" == response.data.status) {
            console.log("dllConstant", response.data);
          }
        })
        .catch(function(error) {
          alert(error);
        });

猜你喜欢

转载自www.cnblogs.com/yamiya/p/12339819.html