我想使用jQuery Ajax将我的页面中的数据数组发送到MVC Action。这里是我的jQuery代码:Ajax中的POST数据大小是否有限制?
$('#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" });
}
UPDATE:找到一个解决办法
JSON的最大长度!我需要增加这个值。在web.config中添加以下内容:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
错误的原因是别的。使用Firebug /使用Visual Studio的断点看到你真正的问题是什么 – Shyju
您将需要返回代码从调试警报的/ dev-工具 – mschr
Guuys最低。请。我仍然需要帮助! – Bryuk