微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Ajax中的POST数据大小是否有限制?

我正在尝试使用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中,我有一个遍历表的循环,我存储结果数组中每一行的数据.然后我使用Ajax将这些数据传递给MVC Action.

这就是我的问题开始的地方……我已经意识到有时它应该如此,但有时我从Ajax警报中得到一个错误(“发生错误!!!”);现在我已经明白,当我的结果数组变大时会发生这个错误.例如:如果它包含100-150个项目>一切都很好,但是当有超过150的时候错误.

Ajax中是否有POST限制?如何设置任何尺寸?我真的需要这个功能!请帮忙!

我的ActionResult代码

public ActionResult SaveQBMatter(QBMatter[] Matters, string originalRecords, string originalHours, string excludedHours, string totalHours) {
  @R_303_4044@ dba = new @R_303_4044@();
  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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐