美文网首页
Vue之axios请求数据

Vue之axios请求数据

作者: 吴国友 | 来源:发表于2021-03-01 16:19 被阅读0次

    引入文件

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

    测试接口
    https://api.coindesk.com/v1/bpi/currentprice.json

    代码块:
    1、get请求:

            var params = {
                    locale: 1,
                };
                // 向具有指定ID的用户发出请求
                axios.get('/user?ID=12345')
                    .then(function (response) {
                        console.log(response);
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
                // 也可以通过 params 对象传递参数
                axios.get('/user',{params:params}).then(function (res) {
                    console.log(res)
                }).catch(function (err) {
                    console.log(err);
                })
    

    2、post请求:

    axios.post('/url', {
                    sex: '1',
                    age: '2'
                })
                    .then(function (res) {
                        console.log(res);
                    })
                    .catch(function (err) {
                        console.log(err);
                    });
    

    3、过个并发请求:

    function getOne() {
                    return $http.get('/url/1');
                }
                function getSecond() {
                    return $http.get('/url/2/secondUrl');
                }
                axios.all([getOne(), getSecond()])
                    .then(axios.spread(function (success, perms) {
                        //两个请求现已完成
                    }));
    

    相关文章

      网友评论

          本文标题:Vue之axios请求数据

          本文链接:https://www.haomeiwen.com/subject/gcuafltx.html