axios post数组数据

新手上路,请多包涵

我正在尝试向我没有太多控制权的服务器发送发布请求。我唯一知道的是,如果我在 Postman 中发布以下数据,我可以获得正确的响应

x-www-form-urlencoded radio button checked

Entered the following 2 array data:
    product_id_list[]          pid1234
    product_id_list[]          pid1235

Header - Content-Type: application/x-www-form-urlencoded

Method: Post

但是当我尝试通过 axios 进行操作时,似乎无法通过正确的参数数据。我试过了

axios.post('https://test.com/api/get_product,
    querystring.stringify({
      'product_id_list': ['pid1234', 'pid1235']
    }))
.
.
.
axios.post('https://test.com/api/get_product,
    querystring.stringify({
      'product_id_list[]': 'pid1234',
      'product_id_list[]': 'pid1235'
    }))
.
.
.

有人知道如何在 axios 中翻译这种类型的数组数据吗?

原文由 Rick Lee 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 527
1 个回答

这个问题也出现在我身上,我用 new FormData() 解决了它。

有一个数组:

 const product_id_list = ['pid1234', 'pid1235']

const bodyFormData = new FormData();

product_id_list.forEach((item) => {
    bodyFormData.append('product_id_list[]', item);
});

axios.post('https://test.com/api/get_product', bodyFormData)

以这种方式将请求发送为 application/x-www-form-urlencoded,并在正文中发送适当的数据。

原文由 David 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题