javascript - Ajax 中的 POST 数据大小有限制吗?

标签 javascript jquery ajax asp.net-mvc

我正在尝试使用 jQuery Ajax 将一组数据从我的页面发送到 MVC 操作。这是我的 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 操作中。

这就是我的问题开始的地方......我意识到有时它应该按预期进行,但有时我会从 Ajax alert("An error has occurred!!!"); 现在我明白了当我的 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/

相关文章:

javascript - 如何在 webkit2gtk 中将 native 代码公开给 javascript

javascript - jQuery 从数组中的数组中获取特定元素

javascript - Laravel Ajax Post 返回 500

javascript - Jquery 异步调用不适用于用户事件

javascript - 附加表的位置不完全显示它在哪里

javascript - JQuery AJAX 同步请求行为

jquery - 向特定图像添加类

javascript - 多次ajax调用后执行函数

javascript - Facebook API : checking if user is logged in on connect

javascript - 普通 JavaScript ajaxStop()