我正在尝试使用jQuery Ajax将页面中的数据数组发送到MVC Action。这是我的jQuery代码:
$('#btnSave').click(
function () {
result = [];
$('#tblMatters tbody tr.mattersRow').each(function () {
if (!($(this).hasClass('warning'))) {
var item = {};
if ($(this).find('td.qbmatter > div.dropdown').length > 0) {
item.QBDescription = $(this).find('td.qbmatter > div.dropdown > a').text();
} else {
item.QBDescription = $(this).find('td.qbmatter').text();
}
var id = $(this).find("td:first > a").text();
item.Narrative = $("#collapse" + id).find("div.scrollCell").text();
item.WorkDate = $(this).find('td.workDate').text();
item.Hours = $(this).find('td.hours').text();
item.Person = $(this).find('td.person').text();
if ($(this).find('td.rate > div.dropdown').length > 0) {
item.Rate = $(this).find('td.rate > div.dropdown > a').text();
} else {
item.Rate = $(this).find('td.rate').text();
}
item.Amount = $(this).find('td.amount').text();
result.push(item);
}
});
var originalRecords = $("#tblSummary tr.summaryTotalRow td.summaryOriginalRecords").text();
var originalHours = $("#tblSummary tr.summaryTotalRow td.summaryOriginalHours").text();
var excludedHours = $("#tblSummary tr.summaryTotalRow td.summaryExcludedHours").text();
var totalHours = $("#tblSummary tr.summaryTotalRow td.summaryTotalHours").text();
$.ajax({
url: "/Home/SaveQBMatter",
type: "POST",
data: JSON.stringify({ 'Matters': result, 'originalRecords': originalRecords, 'originalHours': originalHours, 'excludedHours': excludedHours, 'totalHours': totalHours }),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.status == "Success") {
alert("Success!");
var url = '@Url.Action("Index", "Home")';
window.location.href = url;
} else {
alert("Error On the DB Level!");
}
},
error: function () {
alert("An error has occured!!!");
}
});
});
让我解释一下。我有一个动态生成的HTML表,我需要将此数据存储到数据库中。在jQuery中,我有一个遍历表的循环,并将每一行的数据存储在
result
数组中。然后,我使用Ajax将这些数据传递到MVC Action中。这是我的问题开始的地方...我已经意识到有时会按预期进行,但是有时我会从Ajax
alert("An error has occured!!!");
中获取错误现在我已经了解到,当我的result
数组变大时会发生此错误。例如:如果包含100-150个项目>一切正常,但是当数量超过〜150个时>错误。Ajax中是否有任何POST限制?如何设置任何尺寸?我真的需要这个功能!请帮忙!
我的ActionResult代码:
public ActionResult SaveQBMatter(QBMatter[] Matters, string originalRecords, string originalHours, string excludedHours, string totalHours) {
DBAccess dba = new DBAccess();
int QBMatterID = 0;
int exportedFileID = 0;
foreach (QBMatter qb in Matters) {
dba.InsertQBMatter(qb.QBDescription, qb.Narrative, qb.WorkDate, qb.Person, qb.Hours, qb.Rate, qb.Amount, ref QBMatterID);
}
ExcelTranslator translator = new ExcelTranslator();
translator.CreateExcelFile("", Matters, originalRecords, originalHours, excludedHours, totalHours);
return Json(new { status = "Success", message = "Passed" });
}
更新:找到了解决方案
JSON的最大长度!我需要增加这个值。在web.config中添加以下内容:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
最佳答案
JSON的最大长度!我需要增加这个值。在web.config中添加以下内容:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
关于javascript - Ajax中的POST数据大小是否有限制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20249587/