我的目标是将表日期转换为数组,并使用Ajax post将数组发送到服务器端。这是我第一次使用Ajax帖子,我已经关注了上一篇文章中的所有答案。我仍然不知道缺少什么。我正在使用body-parser来获取服务器端的数据。我将不胜感激,如果您有另一种更简单的方法将数组发送到服务器端,请给予帮助。尝试打印输出时,当前输出未定义。请在下面查看我的代码:
ejs方面
<table id="cartGrid">
<thead>
<tr>
<th>Item Description</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Ext Price</th>
</tr>
</thead>
<tbody>
<tr><td>Old Lamp</td><td>1</td><td>107.00</td><td>107.00</td>
<tr><td>Blue POst</td><td>2</td><td>7.00</td><td>14.00</td>
</tbody>
</table>
<script>
// convert table to array
var myTableArray = [];
$("table#cartGrid tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
// post the data
$.ajax({
url: "/saler",
type: "POST",
data: myTableArray,
});
});
</script>
服务器端
router.post('/saler', function (req, res, next) {
var myTableArray = req.body.myTableArray;
console.log(myTableArray);
});
app.js
app.use( bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
答案 0 :(得分:1)
您的请求正文中没有{"UserId":"user@domain.com","CloseUrl":"https://<server>:8082","HostEditUrl":"http://<server>/we/wordeditorframe.aspx?ui=1033&rs=1033&dchat=false&hid=1&IsLicensedUser=1&WOPISrc=http://<server>:8082/wopi/files/00000000-0000-0000-0000-000000000000","HostViewUrl":"http://<server>/wv/wordviewerframe.aspx?ui=1033&rs=1033&dchat=false&hid=1&IsLicensedUser=1&WOPISrc=http://<server>:8082/wopi/files/00000000-0000-0000-0000-000000000000","SupportsCoauth":false,"SupportsExtendedLockLength":false,"SupportsFileCreation":false,"SupportsFolders":false,"SupportsGetLock":true,"SupportsLocks":true,"SupportsRename":true,"SupportsScenarioLinks":false,"SupportsSecureStore":false,"SupportsUpdate":true,"SupportsUserInfo":true,"LicensesCheckForEditIsEnabled":true,"ReadOnly":false,"RestrictedWebViewOnly":false,"UserCanAttend":false,"UserCanNotWriteRelative":false,"UserCanPresent":false,"UserCanRename":true,"UserCanWrite":true,"WebEditingDisabled":false,"Actions":null,"id":"00000000-0000-0000-0000-000000000000","LockValue":null,"LockExpires":null,"OwnerId":"user@domain.com","BaseFileName":"Test.docx","Container":null,"Size":102400,"Version":1,"UserInfo":null}
。
除非发送数据,否则请执行以下操作:
myTableArray
然后您可以执行$.ajax({
url: "/saler",
type: "POST",
data: {myTableArray},
});
});
来阅读req.body.myTableArray
。
首先尝试myTableArray
来检查您的请求中的数据结构。