Axios

是一个ajax请求库,类似jquery的ajax

执行 GET 请求

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 可选地,上面的请求可以这样做
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

执行 POST 请求

var config = {
       headers: {
             'Content-Type': 'application/x-www-form-urlencoded'
       },
       responseType: 'json',// default
};

axios.post('/yzh/inter/login', {
    userName: this.ruleForm.username,
    passWord: this.ruleForm.password                           
},config)
.then((res) => {
    //if (res) {
      //state.username = res.data.data
      console.log("haha",res)
   // }
})
 

可以通过向 axios 传递相关配置来创建请求

axios(config)
// 发送 POST 请求
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
axios(url[, config])
// 发送 GET 请求(默认的方法)
axios('/user/12345');

请求方法的别名

为方便起见,为所有支持的请求方法提供了别名

axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

在使用别名方法时, urlmethoddata 这些属性都不必在配置中指定。

原文地址:https://www.cnblogs.com/zhaodagang8/p/7822252.html