Vue之axios请求数据

  引入文件

<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>

 

  代码块:

  1、get请求:

  

 1             var params = {
 2                 locale: 1,
 3             };
 4             // 向具有指定ID的用户发出请求
 5             axios.get('/user?ID=12345')
 6                 .then(function (response) {
 7                     console.log(response);
 8                 })
 9                 .catch(function (error) {
10                     console.log(error);
11                 });
12 
13 
14             // 也可以通过 params 对象传递参数
15             axios.get('/user',{params:params}).then(function (res) {
16                 console.log(res)
17             }).catch(function (err) {
18                 console.log(err);
19             })    

 

  2、post请求:

  

 1             axios.post('/url', {
 2                 sex: '1',
 3                 age: '2'
 4             })
 5                 .then(function (res) {
 6                     console.log(res);
 7                 })
 8                 .catch(function (err) {
 9                     console.log(err);
10                 });

 

  3、过个并发请求:

  

 1             function getOne() {
 2                 return $http.get('/url/1');
 3             }
 4 
 5             function getSecond() {
 6                 return $http.get('/url/2/secondUrl');
 7             }
 8 
 9             axios.all([getOne(), getSecond()])
10                 .then(axios.spread(function (success, perms) {
11                     //两个请求现已完成
12                 }));

 

posted @ 2019-03-14 10:49  小旺同学  阅读(13749)  评论(0编辑  收藏  举报