javascript - axios post数组数据

标签 javascript reactjs http axios

我正在尝试向我没有太多控制权的服务器发送发布请求。我唯一知道的是,如果我在 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 中转换这种类型的数组数据吗?

最佳答案

我也遇到了这个问题,我用 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,并在正文中发送适当的数据。

关于javascript - axios post数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072255/

相关文章:

javascript - 如何告诉自定义 onSuccess 函数停止 Ant Design 上传组件中的上传微调器?

android - flutter 错误 : type 'int' is not a subtype of type 'String' in type cast

javascript - 如何强制 Puppeteer 等待非常大的 HTML 表中的所有行完全加载并显示在 DOM 中

javascript - 如何删除 HTML5 音频标签中的下载选项?

reactjs - typescript react : Conditionally optional props based on the type of another prop

node.js - Node http get请求无法处理希伯来语内容

javascript - 通过 Javascript 将 cookie 设置为 HttpOnly

Javascript 子字符串搜索

javascript - 从网页中提取源代码中没有的数据

javascript - 警告 : Can't call setState on a component that is not yet mounted