javascript 对象最大大小限制

发布于 2024-11-06 05:41:21 字数 819 浏览 0 评论 0原文

我正在尝试使用 jquery.ajax 方法将 JavaScript 变量传递到服务器端。

我正在尝试创建一个 json 字符串,但是当变量的长度达到 10000 时,不再向该字符串附加数据。

var jsonObj = '{"code":"' + code + '","defaultfile":"' + defaultfile + '","filename":"' + currentFile + '","lstResDef":[';
        $.each(keys, function(i, item) {
            i = i + 1;
            var value = $("#value" + i).val();
            var value = value.replace(/"/g, "\\\"");
            jsonObj = jsonObj + '{';
            jsonObj = jsonObj + '"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + "," + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"';
            jsonObj = jsonObj + '},';
            alert(jsonObj);             
        });          

        jsonObj = jsonObj + ']}';

这里,当var jsonObj的字符长度为10000时,不附加其后面的值。

看起来这有一些限制。

I'm trying to pass a JavaScript variable to the server-side using jquery.ajax method.

I'm trying to create a json string, but when the length of variable reaches 10000, no more data is appended to the string.

var jsonObj = '{"code":"' + code + '","defaultfile":"' + defaultfile + '","filename":"' + currentFile + '","lstResDef":[';
        $.each(keys, function(i, item) {
            i = i + 1;
            var value = $("#value" + i).val();
            var value = value.replace(/"/g, "\\\"");
            jsonObj = jsonObj + '{';
            jsonObj = jsonObj + '"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + "," + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"';
            jsonObj = jsonObj + '},';
            alert(jsonObj);             
        });          

        jsonObj = jsonObj + ']}';

Here, when the character length of the var jsonObj is 10000, the values following that is not appended.

It looks like there is some limit about that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁客 2024-11-13 05:41:21

字符串长度没有这样的限制。可以肯定的是,我刚刚测试创建一个包含 60 MB 的字符串。

问题可能是您在 GET 请求中发送数据,因此数据是在 URL 中发送的。不同的浏览器对 URL 的限制不同,其中 IE 的限制最低,约为 2 kB。为了安全起见,您在 GET 请求中发送的数据不应超过 1 KB。

要发送这么多数据,您必须在 POST 请求中发送。浏览器对帖子的大小没有硬性限制,但服务器对请求的大小有限制。例如,IIS 的默认限制为 4 MB,但如果您需要发送更多数据,则可以调整该限制。

另外,您不应该使用 += 来连接长字符串。对于每次迭代,需要移动的数据越来越多,因此您拥有的项目越多,速度就会变得越来越慢。将字符串放入数组中并立即连接所有项目:

var items = $.map(keys, function(item, i) {
  var value = $("#value" + (i+1)).val().replace(/"/g, "\\\"");
  return
    '{"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + ",'+
    '" + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"}';
});
var jsonObj =
  '{"code":"' + code + '",'+
  '"defaultfile":"' + defaultfile + '",'+
  '"filename":"' + currentFile + '",'+
  '"lstResDef":[' + items.join(',') + ']}';

There is no such limit on the string length. To be certain, I just tested to create a string containing 60 megabyte.

The problem is likely that you are sending the data in a GET request, so it's sent in the URL. Different browsers have different limits for the URL, where IE has the lowest limist of about 2 kB. To be safe, you should never send more data than about a kilobyte in a GET request.

To send that much data, you have to send it in a POST request instead. The browser has no hard limit on the size of a post, but the server has a limit on how large a request can be. IIS for example has a default limit of 4 MB, but it's possible to adjust the limit if you would ever need to send more data than that.

Also, you shouldn't use += to concatenate long strings. For each iteration there is more and more data to move, so it gets slower and slower the more items you have. Put the strings in an array and concatenate all the items at once:

var items = $.map(keys, function(item, i) {
  var value = $("#value" + (i+1)).val().replace(/"/g, "\\\"");
  return
    '{"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + ",'+
    '" + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"}';
});
var jsonObj =
  '{"code":"' + code + '",'+
  '"defaultfile":"' + defaultfile + '",'+
  '"filename":"' + currentFile + '",'+
  '"lstResDef":[' + items.join(',') + ']}';
彡翼 2024-11-13 05:41:21

第一步始终是首先确定问题出在哪里。你的标题和大部分问题似乎表明你在 JavaScript / 浏览器上遇到了相当低的字符串长度限制,这是一个不太可能的低限制。你不。请考虑:

var str;

document.getElementById('theButton').onclick = function() {
  var build, counter;

  if (!str) {
    str = "0123456789";
    build = [];
    for (counter = 0; counter < 900; ++counter) {
      build.push(str);
    }
    str = build.join("");
  }
  else {
    str += str;
  }
  display("str.length = " + str.length);
};

实时复制

重复单击相关按钮会使字符串变得更长。在 Chrome、Firefox、Opera、Safari 和 IE 中,我可以轻松处理长度超过一百万个字符的字符串:

str.length = 9000
str.length = 18000
str.length = 36000
str.length = 72000
str.length = 144000
str.length = 288000
str.length = 576000
str.length = 1152000
str.length = 2304000
str.length = 4608000
str.length = 9216000
str.length = 18432000

...而且我很确定我可以得到比以上更高的很多那。

所以这与 JavaScript 中的长度限制无关。您尚未显示用于将数据发送到服务器的代码,但很可能您正在使用 GET,这意味着您遇到了 GET 请求的长度限制,因为 GET 参数放入查询字符串中。 详细信息请参见此处

您需要改用 POST。在 POST 请求中,数据位于请求正文中而不是 URL 中,并且数据确实可能非常非常大。

Step 1 is always to first determine where the problem lies. Your title and most of your question seem to suggest that you're running into quite a low length limit on the length of a string in JavaScript / on browsers, an improbably low limit. You're not. Consider:

var str;

document.getElementById('theButton').onclick = function() {
  var build, counter;

  if (!str) {
    str = "0123456789";
    build = [];
    for (counter = 0; counter < 900; ++counter) {
      build.push(str);
    }
    str = build.join("");
  }
  else {
    str += str;
  }
  display("str.length = " + str.length);
};

Live copy

Repeatedly clicking the relevant button keeps making the string longer. With Chrome, Firefox, Opera, Safari, and IE, I've had no trouble with strings more than a million characters long:

str.length = 9000
str.length = 18000
str.length = 36000
str.length = 72000
str.length = 144000
str.length = 288000
str.length = 576000
str.length = 1152000
str.length = 2304000
str.length = 4608000
str.length = 9216000
str.length = 18432000

...and I'm quite sure I could got a lot higher than that.

So it's nothing to do with a length limit in JavaScript. You haven't show your code for sending the data to the server, but most likely you're using GET which means you're running into the length limit of a GET request, because GET parameters are put in the query string. Details here.

You need to switch to using POST instead. In a POST request, the data is in the body of the request rather than in the URL, and can be very, very large indeed.

凉墨 2024-11-13 05:41:21

你必须将其放入 web.config 中:

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

you have to put this in web.config :

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000" />
      </webServices>
    </scripting>
  </system.web.extensions>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文