Javascript axios 发布数组数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45072255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
axios post array data
提问by Rick Lee
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
我正在尝试将发布请求发送到我对其没有太多控制权的服务器。我唯一知道的是,如果我在 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
But when I tried to do it through axios, it doesn't seems the correct params data can get through. I've tried
但是当我尝试通过 axios 执行此操作时,似乎无法通过正确的 params 数据。我试过了
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'
}))
.
.
.
Anyone got an idea on how to translate this type of array data in axios?
有人知道如何在 axios 中转换这种类型的数组数据吗?
回答by Jefree Sujit
You can use the native axiosto create a request. You can pass your payload with the datakey.
您可以使用本机axios来创建请求。您可以使用data密钥传递您的有效负载。
import axios from 'axios';
let payload = {
product_id_list: ['pid1234', 'pid1235']
};
axios({
url: 'https://test.com/api/get_product',
method: 'post',
data: payload
})
.then(function (response) {
// your action after success
console.log(response);
})
.catch(function (error) {
// your action on error success
console.log(error);
});
You can try running your axios code from your browser here.
您可以尝试在此处从浏览器运行 axios 代码。
回答by David
This issue popped up for me as well, and I solved it with new FormData().
这个问题也出现在我身上,我用new FormData().
Having an array:
有一个数组:
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)
Doing it this way sent it the request as application/x-www-form-urlencoded, and the proper data in the body.
以这种方式将请求发送为 application/x-www-form-urlencoded,并在正文中发送正确的数据。
回答by Julio Vargas
$.ajax({
type: "POST",
url: "proceso.php",
data: {'array': array,'array2': array2},
success: function(data){
console.log(data);
}
});
回答by Sotiris Kiritsis
You can try the following:
您可以尝试以下操作:
var payload = {
product_id_list: [
'pid1234',
'pid1235'
]
};
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
payloaxios.post('https://test.com/api/get_product', payload)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Also, you should take a good look to axios documentation.
此外,您应该好好看看axios 文档。

