javascript 对象最大大小限制
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串长度没有这样的限制。可以肯定的是,我刚刚测试创建一个包含 60 MB 的字符串。
问题可能是您在 GET 请求中发送数据,因此数据是在 URL 中发送的。不同的浏览器对 URL 的限制不同,其中 IE 的限制最低,约为 2 kB。为了安全起见,您在 GET 请求中发送的数据不应超过 1 KB。
要发送这么多数据,您必须在 POST 请求中发送。浏览器对帖子的大小没有硬性限制,但服务器对请求的大小有限制。例如,IIS 的默认限制为 4 MB,但如果您需要发送更多数据,则可以调整该限制。
另外,您不应该使用 += 来连接长字符串。对于每次迭代,需要移动的数据越来越多,因此您拥有的项目越多,速度就会变得越来越慢。将字符串放入数组中并立即连接所有项目:
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:
第一步始终是首先确定问题出在哪里。你的标题和大部分问题似乎表明你在 JavaScript / 浏览器上遇到了相当低的字符串长度限制,这是一个不太可能的低限制。你不。请考虑:
实时复制
重复单击相关按钮会使字符串变得更长。在 Chrome、Firefox、Opera、Safari 和 IE 中,我可以轻松处理长度超过一百万个字符的字符串:
...而且我很确定我可以得到比以上更高的很多那。
所以这与 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:
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:
...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, becauseGET
parameters are put in the query string. Details here.You need to switch to using
POST
instead. In aPOST
request, the data is in the body of the request rather than in the URL, and can be very, very large indeed.你必须将其放入 web.config 中:
you have to put this in web.config :