react&vue中发起异步请求之axios详解

初心-杨瑞超个人博客诚邀您加入qq群(IT-程序猿-技术交流群):757345416

安装

npm安装

npm install axios

cdn引入

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

发起请求

get

//方法一

axios.get('www.chao99.top?id=699')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

//方法二

axios.get('www.chao99.top', {
    params: {
      id: 699
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

post

axios.post('www.chao99.top', {
    link: 'www.chao99.top'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

多个

function getChaoPay() {
  return axios.get('www.chao99.top/pay');
}

function getChaoTime() {
  return axios.get('www.chao99.top/time');
}

axios.all([getChaoPay(), getChaoTime()])
  .then(axios.spread(function (acct, perms) {
    console.log('请求完毕')
  }));

// ---------或者------

// 所有请求成功之后,会收到一个数组,包含着响应对象,其中的顺序和请求发送的顺序相同,可使用 axios.spread 分割成多个单独的响应数据
axios.all([

    axios.get('www.chao99.top/pay'),

    axios.get('www.chao99.top/time')

  ])

  .then(axios.spread(function (getChaoPay, getChaoTime) {

    // 上面两个请求都完成后,才执行这个回调方法

    console.log('getChaoPay', getChaoPay.data);

    console.log('getChaoTime', getChaoTime.data);
  }));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值