axios发布数组数据 [英] axios post array data
问题描述
我正在尝试将发帖请求发送到我没有太多控制权的服务器.我唯一知道的是,如果我在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进行操作时,似乎无法获得正确的参数数据.我已经尝试过
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屋!