Javascript javascript对象最大大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5926263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript object max size limit
提问by Paras
I'm trying to pass a JavaScript variable to the server-side using jquery.ajax
method.
我正在尝试将 JavaScript 变量传递给服务器端 usingjquery.ajax
方法。
I'm trying to create a json string, but when the length of variable reaches 10000, no more data is appended to the string.
我正在尝试创建一个 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 + ']}';
Here, when the character length of the var jsonObj is 10000, the values following that is not appended.
这里,当 var jsonObj 的字符长度为 10000 时,不附加后面的值。
It looks like there is some limit about that.
看起来这有一些限制。
回答by Guffa
There is no such limit on the string length. To be certain, I just tested to create a string containing 60 megabyte.
字符串长度没有这样的限制。可以肯定的是,我刚刚测试创建了一个包含 60 兆字节的字符串。
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.
问题很可能是您在 GET 请求中发送数据,因此它是在 URL 中发送的。不同的浏览器对 URL 有不同的限制,其中 IE 的最低限制约为 2 kB。为安全起见,在 GET 请求中发送的数据永远不应超过大约 1 KB。
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.
要发送这么多数据,您必须改为在 POST 请求中发送它。浏览器对帖子的大小没有硬性限制,但服务器对请求的大小有限制。例如,IIS 的默认限制为 4 MB,但如果您需要发送更多数据,则可以调整限制。
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(',') + ']}';
回答by T.J. Crowder
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:
第一步总是首先确定问题所在。您的标题和您的大部分问题似乎表明您在 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);
};
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:
反复单击相关按钮会使字符串变长。使用 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
...and I'm quite sure I could got a lothigher 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.
所以这与 JavaScript 中的长度限制无关。您还没有显示用于将数据发送到服务器的代码,但很可能您正在使用GET
这意味着您遇到了 GET 请求的长度限制,因为GET
参数放在查询字符串中。详情请看这里。
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.
您需要改为使用POST
。在POST
请求中,数据在请求正文中而不是在 URL 中,并且确实可以非常非常大。
回答by Asfour
you have to put this in web.config :
你必须把它放在 web.config 中:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000" />
</webServices>
</scripting>
</system.web.extensions>