我正在尝试使用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 发送,然后在服务器上解析它。
关于Javascript 发布数组并在服务器端接收它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474362/