Vue之axios请求数据

引入文件


测试接口
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请求数据)