我正在尝试向我没有太多控制权的服务器发送发布请求。我唯一知道的是,如果我在 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/