axios.post传参问题

 

传参代码如下,

请求头content-type: application/x-www-form-urlencoded。

export function orderSubmit(data) {
    return request.post(config.router + '/v1/order/unified', qs.stringify(data), {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
    })
}

然后格式是下面这样

而我需要的格式是下面这样

 

 

最后修改,使用transformRequest来改

export function orderSubmit(data) {
    return request.post(config.router + '/v1/order/unified', data, {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        transformRequest: [function (data) {
            return qs.stringify(data)
        }]
    })
}

然后传参Ok了,但是依然有问题,红框部分格式有问题

(ps:也就是请求头是content-type: application/x-www-form-urlencoded时,传的参数必须用qs.stingify转一下。否则后台接不到值或者格式不对)

 

 解决方法,用JSON.stringify单独包裹customized字段

   customized: JSON.stringify([
          {
            region_id: this.detailParams.customized[0].region_id,
            region_no: this.detailParams.customized[0].region_no,
            material_no: this.detailParams.customized[0].material_no,
          },
        ]),

结果如下,ok,搞定

 

二、传formData的请求头内容格式

 

参考:https://juejin.cn/post/7112270574486814727

posted @ 2021-03-26 11:10  飞向火星  阅读(376)  评论(0编辑  收藏  举报