vue中axios处理http发送请求的示例(Post和get)

本篇文章主要介绍了vue中axios处理http请求的示例(Post和get),这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 

本文介绍了vue中axios处理http发送请求的示例(Post和get),分享给大家,具体如下:

axios中文文档  

https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format   axios文档

在处理http请求方面,已经不推荐使用vue-resource了,而是使用最新的axios,下面做一个简单的介绍。

安装

使用node

?

1

npm 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

// Make a request for a user with a given ID

axios.get('/user?ID=12345')

 .then(function (response) {

  console.log(response);

 })

 .catch(function (error) {

  console.log(error);

 });

 

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

 });

Post请求

?

1

2

3

4

5

6

7

8

9

10

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

 }));

这个的使用方法其实和原生的ajax是一样的,一看就懂。

使用 application/x-www-urlencoded 形式的post请求:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

var qs = require('qs');

 axios.post('/bbg/goods/get_goods_list_wechat', qs.stringify({"data": JSON.stringify({

  "isSingle": 1,

  "sbid": 13729792,

  "catalog3": 45908012,

  "offset": 0,

  "pageSize": 25

 })}), {

  headers: {

   "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6",

  }

 })

 .then(function (response) {

  // if (response.data.code == 626) {

   console.log(response);

  // }

 }).catch(function (error) {

  console.log(error);

 });

具体使用参考文档: https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format

注意: 对于post请求,一般情况下,第一个参数是url,第二个参数是要发送的请求体的数据,第三个参数是对请求的配置。

另外:axios默认是application/json格式的,如果不适用 qs.stringify 这种形式, 即使添加了请求头 最后的content-type的形式还是 json 的。

对于post请求,我们也可以使用下面的jquery的ajax来实现:

?

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

$.ajax({

 url:'api/bbg/goods/get_goods_list_wechat',

 data:{

  'data': JSON.stringify({

        "isSingle": 1,

        "sbid": 13729792,

        "catalog3": 45908012,

        "offset": 0,

        "pageSize": 25

      })   

 }, 

 beforeSend: function(request) {

  request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");

 },

 type:'post',

 dataType:'json',

 success:function(data){  

  console.log(data);

 },

 error: function (error) {

  console.log(err);

 },

 complete: function () {

 

 }

});

显然,通过比较,可以发现,jquery的请求形式更简单一些,且jqury默认的数据格式就是 application/x-www-urlencoded ,从这方面来讲会更加方便一些。

另外,对于两个同样的请求,即使都请求成功了,但是两者请求得到的结果也是不一样的,如下:

不难看到: 使用axios返回的结果会比jquery的ajax返回的结构(实际的结果)多包装了一层,包括相关的config、 headers、request等。

对于get请求, 我个人还是推荐使用axios.get()的形式,如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

axios.get('/bbg/shop/get_classify', {

 params: {

  sid: 13729792

 },

 headers: {

  "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6"

 }

})

.then(function (response) {

 if (response.data.code == 130) {

  items = response.data.data;

  store.commit('update', items);

  console.log(items);

 }

 console.log(response.data.code);

}).catch(function (error) {

 console.log(error);

 console.log(this);

});

即第一个参数是:url, 第二个参数就是一个配置对象,我们可以在配置对象中设置 params 来传递参数。

个人理解为什么get没有第二个参数作为传递的查询字符串,而post有第二个参数作为post的数据。

因为get可以没有查询字符串,也可以get请求,但是post必须要有post的数据,要不然就没有使用post的必要了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

原文链接:http://www.cnblogs.com/zhuzhenwei918/p/6869330.html

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值