简体   繁体   English

是否对Ajax中的POST数据大小有任何限制?

[英]Is it any limit for POST data size in Ajax?

I'm trying to send an array of data from my page to the MVC Action using jQuery Ajax. 我正在尝试使用jQuery Ajax从我的页面向MVC Action发送一组数据。 Here is my jQuery code: 这是我的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!!!");
      }
    });
});

Let me explain a little bit. 让我解释一下。 I have an HTML table that was built dynamically and I need to store this data into a database. 我有一个动态构建的HTML表,我需要将这些数据存储到数据库中。 In jQuery I have a loop going through the table and I store data of every row in the result array. 在jQuery中,我有一个遍历表的循环,我存储result数组中每一行的数据。 Then I pass this data using Ajax into MVC Action. 然后我使用Ajax将这些数据传递给MVC Action。

And here is where my problem starts... I've realized that sometimes it goes as it should be, but sometimes I'm getting an error from Ajax alert("An error has occured!!!"); 这就是我的问题开始的地方......我已经意识到有时它应该如此,但有时我从Ajax alert("An error has occured!!!");得到一个错误alert("An error has occured!!!"); Now I've understood that this error occurs when my result array is getting big. 现在我已经明白,当我的result数组变大时会发生这个错误。 For example: If it contains 100-150 items > everything is good, but when there are more than ~150 > Error. 例如:如果它包含100-150个项目>一切都很好,但是当有超过150个>错误时。

Is there any POST limit in Ajax? Ajax中是否有POST限制? How can I set it up for any sizes? 如何设置任何尺寸? I really need this functionality! 我真的需要这个功能! Any help please! 请帮忙!

My ActionResult Code: 我的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: Found a solution 更新:找到解决方案

JSON has a maximum length! JSON有一个最大长度! I need to increase this value. 我需要增加这个值。 In web.config add the following: 在web.config中添加以下内容:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

JSON has a maximum length! JSON有一个最大长度! I need to increase this value. 我需要增加这个值。 In web.config add the following: 在web.config中添加以下内容:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

Is it any POST Limit in Ajax? 它是Ajax中的任何POST限制吗?

No, the HTTP specification doesn't impose a specific size limit for posts. 不,HTTP规范没有对帖子强加特定的大小限制。 However it depends on the Web Server which you are using or the programming technology used to process the form submission. 但是,它取决于您使用的Web服务器或用于处理表单提交的编程技术。

In ASP.NET MVC you can try this: 在ASP.NET MVC中,您可以尝试这样做:

<system.webServer>
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1000000" />
    </requestFiltering>
</security>

The HTTP spec doesn't defining a limitation of POST data size. HTTP规范没有定义POST数据大小的限制。

But using ASP.NET MVC, there could be a POST data limitation, try to increase it in your Web.Config file: 但是使用ASP.NET MVC时, 可能存在POST数据限制,请尝试在Web.Config文件中增加它:

<system.webServer>
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1000000" />
    </requestFiltering>
</security>

From MSDN : 来自MSDN

Specifies the maximum length of content in a request, in bytes. 指定请求中内容的最大长度(以字节为单位)。 The default value is 30000000. 默认值为30000000。

IF there is a client-side limit then it would be browser specific but the HTTP spec does not define a limitation of POST data size. 如果存在客户端限制,那么它将是浏览器特定的,但HTTP规范没有定义POST数据大小的限制。

Keep in mind that a POST is merely bytes across the network so the more fields you are posting then the longer it can take to upload that data. 请记住,POST只是网络中的字节数,因此您发布的字段越多,上传该数据所需的时间就越长。

A 1MB POST is going to make the user feel like the form is broken and unresponsive if using a traditional form submit. 如果使用传统的表单提交,1MB的POST将使用户感觉表单已损坏且无响应。

If there are a lot of fields to serialize() then AJAX could hang up the browser while it collects all of the data. 如果有许多字段要serialize()那么AJAX可以在收集所有数据时挂断浏览器。 I think browsers do have a memory limit for JavaScript overall so if you hit that limit then the AJAX process will fail. 我认为浏览器总体上对JavaScript有一个内存限制,所以如果你达到了这个限制,那么AJAX过程就会失败。

// wanna have some fun?
var html = '<div></div>';

for(var i = 0; i < 1000000; i++){
    html += html;
}

Your best bet is to increase the maximum allowable POST size on the server-side to avoid issues. 最好的办法是增加服务器端允许的最大POST大小,以避免出现问题。

Most commonly issues arise when people make an upload script which simply seems to hang while the user is confused why their 3MB pictures are going slow on a 125KB/s upload link. 最常见的问题出现在人们制作上传脚本时,如果用户感到困惑,他们为什么他们的3MB图片在125KB / s的上传链接上速度慢的话就会挂起。

This solved my problem: 这解决了我的问题:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483647" />
        </webServices>
    </scripting>
</system.web.extensions>

The limit is set through a server 'max_post_size' or similar naming. 限制是通过服务器'max_post_size'或类似命名设置的。 Typical default server settings are 2-8 MB in one post. 一个帖子中典型的默认服务器设置为2-8 MB。

On the client side, only GET has a maximum limit, generally considered 1500 bytes (minus 30-150 from request headers) and the reason is the MTU in the more lowlevel network hardware 在客户端,只有GET有一个最大限制,通常被认为是1500字节(从请求头减去30-150),原因是更低级网络硬件中的MTU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM