Javascript发布数组并在服务器端接收它

Javascript post array and receive it in serverside

本文关键字:服务器 端接 布数组 数组 Javascript      更新时间:2023-09-26

我正在尝试使用 ajax 发送数组,想象一下数组

var anotherOne = 'anotherOneData';
var documents = ['banana', 'apple', 'monkey'];

我使用 ajax 将这些正常值和数组发送到服务器端,在服务器端得到这个没有问题,

console.log(req.body);
//result (anotherOne is I sent too, this is not array,)
{ anotherOne : 'anotherOneData',
  'documents[]': 
[ 'banana', 
'apple, 
'monkey' ] }

如何访问文档的第一个元素?

req.body.documents[0] // not working
req.body.documents[0][0] // not working
req.body.documents instanceof Array // false : why?

完全无法理解发生了什么,请帮助我。

根据您记录的内容,req.body上的属性称为 documents[] ,因此:

console.log(req.body['documents[]'][0]);

也就是说,我想我会将其作为 JSON 发送,然后在服务器上解析它。