axios参数传递

axios的参数传递方式
1:get

Axios.get('url', {
    params: {
        id: 123,
        name: 'DAN',
    }
})

2:post

Axios.post('url', {
        id: 123,
        name: 'DAN',
})

3:put

Axios.put('url', {
        id: 123,
        name: 'DAN',
})

或者我们之间写data:{
id: 123,
name: ‘DAN’,
}
或者data:{
A
}

数据返回中A:{
id: 123,
name: ‘DAN’,
}
但其实还会有很多问题
我们传递的参数在get方法下显示什么都没传递,这一点我现在还没搞清楚明白,有空会看看

还有就是传递的参数会以{id:“123”,name:“DAN”}的形式传递而不是formdata形式
将其转化就是我们要做的事情
这里就我了解的有两种方法
1:引入import Qs from ‘qs’

在此操作前先安装QS npm install --save qs
然后引入import Qs from ‘qs’

axios({
  url: '/user',
  method: 'post',
  data: {
        id: 123,
        name: 'DAN',
  },
  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
  headers:{'Content-Type':'application/x-www-form-urlencoded'}
})
})

其中加上

  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
  headers:{'Content-Type':'application/x-www-form-urlencoded'}

才是有用的
2:

axios({
url: '/user',
method: 'post',
data: {
      id: 123,
      name: 'DAN',
},
transformRequest: [function (data) {
  // Do whatever you want to transform the data
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  return ret
}],
headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
}
})

其中

 transformRequest: [function (data) {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }],
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }

这样就可以产生formdata

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值