axios

全局配置

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// 解决post方式提交数据失败问题
axios.defaults.transformRequest=[function (data, headers) {
    // Do whatever you want to transform the data
    // 可以对post提交的参数对象进行转换, 转换成查询字符串  {name:'zs',age:19}=>name=zs&age=19
    var params='';
    for(var key in data){
      params[key]=data[key]+'&';
    }
    // 去掉最后的'&'
    params=params.substr(params,params.length-1);
    return params;
  }];

axios发送get请求

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

发送post请求

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

参考文档

看云
npm

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

推荐阅读更多精彩内容