axios网络请求的各种的写法

做法1: 为axios传递相关配置来创建请求。可以自行决定是post或是get或是put或是delete等

// 发送 POST 请求
axios({
  method: 'post',
  url: '/user/29719',
  data: {
    name: 'coderZb',
    sex: '1'
  }
});

做法2: axios 的get请求

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=29719')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

//  可选地,上面的请求可以这样做
axios.get('/user', {
    params: {
      ID: 29719
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

做法2: axios 的post请求

axios.post('/user', {
    name: 'coderZb',
    sex: '1'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

切记不要混了,最主要的区别就是可选择请求类型等自定义参数的的axios请求的写法是axios({}); get请求的写法是axios.get();post请求的写法是axios.post()

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