Axios简介

安装

1
$ npm install axios
1
$ bower install axios

CDN配置

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

示例

  • GET示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    const axios = require('axios');

    // Make a request for a user with a given ID
    axios.get('/user?ID=12345')
    .then(function (response) {
    // handle success
    console.log(response);
    })
    .catch(function (error) {
    // handle error
    console.log(error);
    })
    .then(function () {
    // always executed
    });

    // Optionally the request above could also be done as
    axios.get('/user', {
    params: {
    ID: 12345
    }
    })
    .then(function (response) {
    console.log(response);
    })
    .catch(function (error) {
    console.log(error);
    })
    .then(function () {
    // always executed
    });

    // Want to use async/await? Add the `async` keyword to your outer function/method.
    async function getUser() {
    try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
    } catch (error) {
    console.error(error);
    }
    }
  • POST示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
    })
    .then(function (response) {
    console.log(response);
    })
    .catch(function (error) {
    console.log(error);
    });

  • 多个请求示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    function getUserAccount() {
    return axios.get('/user/12345');
    }

    function getUserPermissions() {
    return axios.get('/user/12345/permissions');
    }

    axios.all([getUserAccount(), getUserPermissions()])
    .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
    }));

更多相关示例可以查看官网:https://github.com/axios/axios