如何使用Javascript和节点js从表单传递数组
我正在使用表格收集数据。表单中包含一个部分,其中有一个t实体,并且在开始时是空的。使用很少的函数,就可以用数组中的数据填充这个tbody。在js文件中访问该数组。完成表单的所有方面后,表单中的大部分数据将发送到我的路由文件如何使用Javascript和节点js从表单传递数组,javascript,jquery,node.js,Javascript,Jquery,Node.js,我正在使用表格收集数据。表单中包含一个部分,其中有一个t实体,并且在开始时是空的。使用很少的函数,就可以用数组中的数据填充这个tbody。在js文件中访问该数组。完成表单的所有方面后,表单中的大部分数据将发送到我的路由文件index.js中的post请求。大多数数据项都是通过其name属性访问的,例如req.body.nameOfInputField 我应该如何发送req.body中的数组或访问它 main.hbs <form autocomplete="off" action="/pos
index.js
中的post请求。大多数数据项都是通过其name
属性访问的,例如req.body.nameOfInputField
我应该如何发送req.body中的数组或访问它
main.hbs
<form autocomplete="off" action="/post-prop" method="post" onsubmit="return validateForm()" id="post-prop-form" enctype="multipart/form-data" class="col-lg-12">
<div class="row">
..
<table id="tableId">
..
<tbody id="bodyId">
</tbody>
..
</table>
..
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
let arr = [];
$(function(){
...
//Various functions called such as add, edit, delete are used for 'arr'
//These changes seen in tbody
}
function validateForm(){
//This function is called onsubmit of form
// What should be placed here or anywhere else
// so I can send my 'arr' to POST route in index.js
}
router.post('/post-prop', isLoggedIn, upload.single('image'), function(req, res, next){
const data = new Data({
data1 : req.body.data1,
data2 : req.body.data2,
array : //What should be placed here for 'arr'
});
});
function validateForm(){
....
const data = {
fields: arr
};
jQuery.ajax({
data: JSON.stringify(data),
content: 'application/json'
});
...
}
router.post('/post-prop', isLoggedIn, upload.single('propImage'), function(req, res, next){
console.log(req.body.fields); //However this comes as undefined
...
}
index.js
<form autocomplete="off" action="/post-prop" method="post" onsubmit="return validateForm()" id="post-prop-form" enctype="multipart/form-data" class="col-lg-12">
<div class="row">
..
<table id="tableId">
..
<tbody id="bodyId">
</tbody>
..
</table>
..
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
let arr = [];
$(function(){
...
//Various functions called such as add, edit, delete are used for 'arr'
//These changes seen in tbody
}
function validateForm(){
//This function is called onsubmit of form
// What should be placed here or anywhere else
// so I can send my 'arr' to POST route in index.js
}
router.post('/post-prop', isLoggedIn, upload.single('image'), function(req, res, next){
const data = new Data({
data1 : req.body.data1,
data2 : req.body.data2,
array : //What should be placed here for 'arr'
});
});
function validateForm(){
....
const data = {
fields: arr
};
jQuery.ajax({
data: JSON.stringify(data),
content: 'application/json'
});
...
}
router.post('/post-prop', isLoggedIn, upload.single('propImage'), function(req, res, next){
console.log(req.body.fields); //However this comes as undefined
...
}
我试着上网,但找不到任何与我的问题有关的东西
更新
我没有使用任何AJAX函数调用。这是从我的表格中发送的一个简单的post请求。myindex.js
中的“数据”模型允许我向模型的属性添加值,因此我只能在数据发送后定义数据
根据@sigfried建议的答案,我在相应的文件中添加了以下代码
main.js
<form autocomplete="off" action="/post-prop" method="post" onsubmit="return validateForm()" id="post-prop-form" enctype="multipart/form-data" class="col-lg-12">
<div class="row">
..
<table id="tableId">
..
<tbody id="bodyId">
</tbody>
..
</table>
..
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
let arr = [];
$(function(){
...
//Various functions called such as add, edit, delete are used for 'arr'
//These changes seen in tbody
}
function validateForm(){
//This function is called onsubmit of form
// What should be placed here or anywhere else
// so I can send my 'arr' to POST route in index.js
}
router.post('/post-prop', isLoggedIn, upload.single('image'), function(req, res, next){
const data = new Data({
data1 : req.body.data1,
data2 : req.body.data2,
array : //What should be placed here for 'arr'
});
});
function validateForm(){
....
const data = {
fields: arr
};
jQuery.ajax({
data: JSON.stringify(data),
content: 'application/json'
});
...
}
router.post('/post-prop', isLoggedIn, upload.single('propImage'), function(req, res, next){
console.log(req.body.fields); //However this comes as undefined
...
}
index.js
<form autocomplete="off" action="/post-prop" method="post" onsubmit="return validateForm()" id="post-prop-form" enctype="multipart/form-data" class="col-lg-12">
<div class="row">
..
<table id="tableId">
..
<tbody id="bodyId">
</tbody>
..
</table>
..
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
let arr = [];
$(function(){
...
//Various functions called such as add, edit, delete are used for 'arr'
//These changes seen in tbody
}
function validateForm(){
//This function is called onsubmit of form
// What should be placed here or anywhere else
// so I can send my 'arr' to POST route in index.js
}
router.post('/post-prop', isLoggedIn, upload.single('image'), function(req, res, next){
const data = new Data({
data1 : req.body.data1,
data2 : req.body.data2,
array : //What should be placed here for 'arr'
});
});
function validateForm(){
....
const data = {
fields: arr
};
jQuery.ajax({
data: JSON.stringify(data),
content: 'application/json'
});
...
}
router.post('/post-prop', isLoggedIn, upload.single('propImage'), function(req, res, next){
console.log(req.body.fields); //However this comes as undefined
...
}
我希望我没有忘记以错误的方式实现它。尝试从多部分表单数据访问和数组有点棘手,但您可以检查req.body并找出答案,在这种情况下,对我来说最好的解决方案是,因为我认为您使用的是jQuery,或者就此而言,原生XMLHttpRequest或Fetch,您可以将表单内容作为应用程序/json发送,快速jQuery示例:
const data = {
fields: [1, 2, 3]
}
jQuery.ajax({
data: JSON.stringify(data),
contentType: 'application/json'
})
然后在您的快速路线上,您可以以非常舒适的方式进入,如下所示:
app.post('/route', function(req, res) {
const fieldsArray = req.body.field;
})
希望这会有所帮助,干杯。您可以简单地为数据创建一个json对象。json对象可能包含嵌套对象、数组等。然后json.stringify数据并将其发送到节点。 设置
内容类型:application/json
在节点级别,您只需执行var obj=JSON.parse(req.body.data)
,它将返回您编码的javascript对象
您可以在此处查看参考资料:如果我的回答回答了您的问题,请检查是否正确。谢谢我正在我的routes文件夹中定义我的“数据”。基于您的解决方案,我不明白我之前应该如何定义它并添加它。同样与jQuery和本机XMLHTttpRequest或Fetch相关,我不太理解您的要求。@rut_0_1抱歉,您是否在express中使用正文解析器模块?是的,我正在使用正文-parser@rut_0_1,检查jQuery.ajax()设置,因为
content
参数是contentType
,默认方法是GET,所以必须将其设置为POST。希望这有帮助,西格弗里德。