axios处理http请求

你的名字 2021-01-11 14:57 972阅读 0赞

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. // Make a request for a user with a given ID
  2. axios.get('/user?ID=12345')
  3. .then(function (response) {
  4. console.log(response);
  5. })
  6. .catch(function (error) {
  7. console.log(error);
  8. });
  9. // Optionally the request above could also be done as
  10. axios.get('/user', {
  11. params: {
  12. ID: 12345
  13. }
  14. })
  15. .then(function (response) {
  16. console.log(response);
  17. })
  18. .catch(function (error) {
  19. console.log(error);
  20. });

Post请求

  1. axios.post('/user', {
  2. firstName: 'Fred',
  3. lastName: 'Flintstone'
  4. })
  5. .then(function (response) {
  6. console.log(response);
  7. })
  8. .catch(function (error) {
  9. console.log(error);
  10. });

同时执行多个请求

  1. function getUserAccount() {
  2. return axios.get('/user/12345');
  3. }
  4. function getUserPermissions() {
  5. return axios.get('/user/12345/permissions');
  6. }
  7. axios.all([getUserAccount(), getUserPermissions()])
  8. .then(axios.spread(function (acct, perms) {
  9. // Both requests are now complete
  10. }));

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

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

  1. var qs = require('qs');
  2. axios.post('/bbg/goods/get_goods_list_wechat', qs.stringify({
  3. "data": JSON.stringify({
  4. "isSingle": 1,
  5. "sbid": 13729792,
  6. "catalog3": 45908012,
  7. "offset": 0,
  8. "pageSize": 25
  9. })}), {
  10. headers: {
  11. "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6",
  12. }
  13. })
  14. .then(function (response) {
  15. // if (response.data.code == 626) {
  16. console.log(response);
  17. // }
  18. }).catch(function (error) {
  19. console.log(error);
  20. });

具体使用参考文档: 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. $.ajax({
  2. url:'api/bbg/goods/get_goods_list_wechat',
  3. data:{
  4. 'data': JSON.stringify({
  5. "isSingle": 1,
  6. "sbid": 13729792,
  7. "catalog3": 45908012,
  8. "offset": 0,
  9. "pageSize": 25
  10. })
  11. },
  12. beforeSend: function(request) {
  13. request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");
  14. },
  15. type:'post',
  16. dataType:'json',
  17. success:function(data){
  18. console.log(data);
  19. },
  20. error: function (error) {
  21. console.log(err);
  22. },
  23. complete: function () {
  24. }
  25. });

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

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

1044137-20170522000140463-504264454.png

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

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

  1. axios.get('/bbg/shop/get_classify', {
  2. params: {
  3. sid: 13729792
  4. },
  5. headers: {
  6. "BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6"
  7. }
  8. })
  9. .then(function (response) {
  10. if (response.data.code == 130) {
  11. items = response.data.data;
  12. store.commit('update', items);
  13. console.log(items);
  14. }
  15. console.log(response.data.code);
  16. }).catch(function (error) {
  17. console.log(error);
  18. console.log(this);
  19. });

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

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

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


发表评论

表情:
评论列表 (有 0 条评论,972人围观)

还没有评论,来说两句吧...

相关阅读

    相关 axios+promise整合http请求

    前言 当以前后端分离的方式进行项目的开发时,我们可以简单地把前端开发看做页面展示的开发,把后端开发看做数据处理的开发。前端页面的展示,会根据需求去后端请求相应的数据。