欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue之axios请求数据

程序员文章站 2022-12-24 13:19:56
引入文件 代码块: 1、get请求: 2、post请求: 3、过个并发请求: ......

  引入文件

<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                 }));