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

标签 javascript jquery ajax node.js

我正在尝试使用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/

相关文章:

javascript - 如何使 Angularjs 路由在 html5mode 上与 expressJs 路由一起工作

javascript - 自动填充电话号码输入jquery的括号和连字符

javascript - 解析 XML catch block 未捕获 JS 中的异常

php - 使用 laravel Controller 从 $.ajax 调用接收数据

javascript - 有 getJSON 成功函数吗?

javascript - 如何通过ajax将输入数据传递给另一个php?

javascript - 我想加快 Nashorn 的评估速度

javascript - React 路由器与 Meteor : How to remove code param from URL after redirect from OAuth without reload

javascript - 循环遍历 amcharts 堆积柱形图的每个对象属性

javascript - 通过 javascript/jQuery 检测 IE 中的箭头键按下