axios

2019-03-07  本文已影响0人  duans_

全局配置

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

上一篇下一篇

猜你喜欢

热点阅读