axios 发布数组数据 [英] axios post array data

查看:18
本文介绍了axios 发布数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send post request to a server I don't have much control on it. The only thing I know is I can obtain the correct response if I post the following data in 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 执行此操作时,似乎无法通过正确的 params 数据.我试过了

But when I tried to do it through axios, it doesn't seems the correct params data can get through. I've tried

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 中转换这种类型的数组数据吗?

Anyone got an idea on how to translate this type of array data in axios?

推荐答案

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

This issue popped up for me as well, and I solved it with 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,并在正文中发送正确的数据.

Doing it this way sent it the request as application/x-www-form-urlencoded, and the proper data in the body.

这篇关于axios 发布数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆