javascript对象最大大小限制 [英] javascript object max size limit

查看:165
本文介绍了javascript对象最大大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 + ']}';

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

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

看起来有一些限制。

推荐答案

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

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

问题很可能是你在GET请求中发送数据,所以它是在URL。不同的浏览器对URL有不同的限制,其中IE具有约2kB的最低限制。为了安全起见,您不应该在GET请求中发送超过大约一千字节的数据。

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.

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

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(',') + ']}';

这篇关于javascript对象最大大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆