使用axios需要注意的地方

点进来的朋友应该是对axios有些基础了解的,我选择把介绍axios的内容放在后面,如果不了解基本语法和如何使用,可以先看后面的内容,再来看需要注意的地方。

一、axios的请求方式不同 请求结果不同

在官文中有两个demo:

1 直接在请求里拼接请求参数get参数
axios.get('/user?ID=12345')
  .then(function(response) {
      console.log(response);
  })
  .catch(function(error) {
      console.log(error);
  })

2 或者使用对象的方式填写params参数
axios.get('/user', {
    params: {
        ID: 12345
      }
  })
  .then(function(response) {
      console.log(response);
  })
  .catch(function(error) {
      console.log(error);
  })

代码看上去是一样的,但是在使用第二种方法的时候,会对参数执行encodeURIComponent
例如:

1 直接在请求理解里面拼接参数get参数
axios.get('/user?testurl=http://test.aaa.com')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

2 或者是使用对象的方式填写params参数
axios.get('/user', {
    params: {
      testurl: 'http://test.aaa.com'
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

encodeURIComponent() 函数的作用就是把字符串(非数字和ASCLL标点符号)作为 URI 组件进行编码。

目前只有这一个注意点

等有时间了补充axios的介绍,话说我还不明白axios.create的使用呢?

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容