8

I am using AJAX to asynchronously call a PHP script that returns a large serialized array of JSON objects (about 75kbs or 80k characters). Every time I try and return it it hits a 3000 character limit. Is there a maximum size set anywhere on servers or within jQuery's ajax implementation?

EDIT: the 3'000 limit is a Chrome limit, FF has a 10'000 character limit and Safari has no limit. I'm guessing there is no fix for this apart from changing my code to split/lessen the return data.

4

1 回答 1

1

您可以拆分您的 JSON 并逐个使用 $.ajax

我为你做了一个例子

html端:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="jquery.js"></script>
    <script>
    $(document).ready(function() {

        window.result_json = {};

       $.get("x.php?part=size",function(data){
          var count = data;
          for (var i = 0; i <= count; i++) {
            $.ajax({
              dataType: "json",
              url: "x.php",
              async: false,
              data: "part="+i,
              success: function(data){
                $.extend(window.result_json,data);
              }
            });
          };
          console.log(window.result_json);
       });
    });
</script>
</head>
<body>

</body>
</html>

PHP端(文件名为x.php):

<?php
if(isset($_GET['part'])){

    function send_part_of_array($array,$part,$moo){
        echo json_encode(array_slice($array, $part * $moo,$moo,true),JSON_FORCE_OBJECT);
    }
    $max_of_one = 3;
    $a = array("a","b","c","d","e","f","g","h","i","j");

    if($_GET['part'] == 'size'){
        echo round(count($a) / $max_of_one);
    }else{
        send_part_of_array($a,$_GET['part'],$max_of_one);
    }

}
?>

首先使用$.get(part=size),检查有多少片。

其次用$.ajax(part=(int)PART-NUMBER) ,在 for 循环中一一获取 JSON 的各个部分

最后$.extend在 for loop marge new 中获取 JSON 和旧的 JSON 元素window.result_json

注意: $max_of_one变量确定要发布的切片数。

于 2013-10-01T02:21:01.820 回答