javascript object max size limit

前端 未结 3 689
有刺的猬
有刺的猬 2020-11-29 06:09

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 varia

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 06:58

    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:

    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);
    };
    

    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:

    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 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, because GET parameters are put in the query string. Details here.

    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.

提交回复
热议问题