41

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 の場合、それ以降の値は付加されません。

それに関しては、ある程度の制限があるようです。

4

3 に答える 3

69

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(',') + ']}';
于 2011-05-08T08:34:36.737 に答える
8

ステップ 1 では常に、まず問題がどこにあるかを特定します。あなたのタイトルとあなたの質問のほとんどは、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 では、100 万文字を超える文字列に問題はありませんでした。

長さ = 9000
長さ = 18000
長さ = 36000
長さ = 72000
長さ = 144000
長さ = 288000
長さ = 576000
長さ = 1152000
長さ = 2304000
長さ = 4608000
長さ = 9216000
長さ = 18432000

...そして、私はそれよりもはるかに高くなることができると確信しています.

したがって、JavaScript の長さ制限とは関係ありません。データをサーバーに送信するためのコードは示していませんが、パラメーターがクエリ文字列に入れられるGETため、GET 要求の長さ制限に達しているという意味を使用している可能性があります。詳細はこちらGET

POST代わりに使用に切り替える必要があります。リクエストでは、POSTデータは URL ではなくリクエストの本文にあり、実際には非常に大きくなる可能性があります。

于 2011-05-08T08:17:48.127 に答える
-9

これを web.config に入れる必要があります:

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000" />
      </webServices>
    </scripting>
  </system.web.extensions>
于 2011-05-08T07:47:17.950 に答える