axios

作者: duans_ | 来源:发表于2019-03-07 16:43 被阅读0次

    全局配置

    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

    相关文章

      网友评论

          本文标题:axios

          本文链接:https://www.haomeiwen.com/subject/hgfhpqtx.html