我正试图使用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 + ']}';
在这里,当变量jsonObj的字符长度为10000时,其后的值不会被添加。
看起来有一些限制。
+=
操作比使用数组的.join()
方法效率更低。 - chrisfrancis27