php JSON 对象最大大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1544807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:49  来源:igfitidea点击:

JSON object max size?

phpjqueryajaxapachejson

提问by

Using jquery, I'm able to send json objects from client to server using ajax like this:

使用 jquery,我可以使用 ajax 将 json 对象从客户端发送到服务器,如下所示:

var strJSON = '{"event":[{
    "dates":[
        "2009-10-14","2009-10-15"],
   "teams":[
        {"id":"0","country":"USA","state":"CA","name":"California Polytechnic State University","subteam":""},
        {"id":"1","country":"USA","state":"CA","name":"California State University, Bakersfield","subteam":""},
        {"id":"2","country":"USA","state":"CA","name":"California State University, Fresno","subteam":""},
        {"id":"3","country":"USA","state":"CA","name":"California State University, Fullerton","subteam":""}]
}]}';

$.ajax({
    url: '../ajax/save_event',
    type: 'POST',
    data: { data : strJSON },
    dataType: 'json',
    timeout: 8000,
    cache: false
});

It works well. But if the JSON string gets much bigger (not sure the exact size, but somewhere around 5x bigger), the $_POST data received by the server is empty. Anybody know why?

它运作良好。但是如果 JSON 字符串变得更大(不确定确切的大小,但大约 5 倍大),服务器收到的 $_POST 数据是空的。有人知道为什么吗?

I'm using Apache/PHP/jquery. It happens from both IE and Firefox. Is there a config setting somewhere I need to adjust?

我正在使用 Apache/PHP/jquery。它发生在 IE 和 Firefox 中。是否有需要调整的配置设置?

Here's an example of a string that fails to make it through:

这是一个无法通过的字符串示例:

{"events":[{"dates":["2009-10-10","2009-10-11","2009-10-12"],"divisions":[{"level":"Collegiate","name":"Varsity","subdivision":"Division I","rounds":[],"teams":[{"id":"0","country":"USA","state":"CA","name":"California Polytechnic State University","subteam":""},{"id":"1","country":"USA","state":"CA","name":"California State University, Bakersfield","subteam":""},{"id":"2","country":"USA","state":"CA","name":"California State University, Fresno","subteam":""},{"id":"3","country":"USA","state":"CA","name":"California State University, Fullerton","subteam":""},{"id":"4","country":"USA","state":"CA","name":"Stanford University","subteam":""},{"id":"5","country":"USA","state":"CA","name":"University of California, Davis","subteam":""},{"id":"6","country":"USA","state":"CA","name":"San Francisco State University","subteam":""},{"id":"7","country":"USA","state":"CA","name":"Lassen Community College","subteam":""},{"id":"8","country":"USA","state":"CA","name":"Menlo College","subteam":""},{"id":"9","country":"USA","state":"CA","name":"Fresno Pacific University","subteam":""},{"id":"10","country":"USA","state":"CA","name":"Bakersfield","subteam":""},{"id":"11","country":"USA","state":"CA","name":"Buchanan","subteam":""},{"id":"12","country":"USA","state":"CA","name":"Campolindo-Moraga","subteam":""},{"id":"13","country":"USA","state":"CA","name":"Fremont-Sunnyvale","subteam":""},{"id":"14","country":"USA","state":"CA","name":"Ponderosa-Shingle Springs","subteam":""},{"id":"15","country":"USA","state":"CA","name":"West Covina","subteam":""},{"id":"16","country":"USA","state":"CA","name":"Gilroy","subteam":""},{"id":"17","country":"USA","state":"CA","name":"San José State University","subteam":""},{"id":"18","country":"USA","state":"CA","name":"University of California, Los Angeles","subteam":""},{"id":"19","country":"USA","state":"CA","name":"Sierra College","subteam":""},{"id":"20","country":"USA","state":"CA","name":"Selma","subteam":""},{"id":"21","country":"USA","state":"CA","name":"Liberty","subteam":""}],}]}]}

It's created using json.org's "stringify":

它是使用 json.org 的“stringify”创建的:

var strJSON = JSON.stringify(oEvent);

EDIT:Investigating further, I changed the dataType to "text" and tried sending a long string of numbers. It works up until ~3500 characters then chokes (and when I say "choke" I mean that the request arrives at the server with a null $_POST.)

编辑:进一步调查,我将数据类型更改为“文本”并尝试发送一长串数字。它一直工作到大约 3500 个字符然后阻塞(当我说“阻塞”时,我的意思是请求以空 $_POST 到达服务器。)

My PHP post_max_size is 64M, so that's not it. Someone suggested using Apache's "LimitRequestBody" which wasn't in httpd.conf, so I added "LimitRequestBody 0" to it and rebooted Apache. That didn't help either.

我的 PHP post_max_size 是 64M,所以不是这样。有人建议使用 httpd.conf 中没有的 Apache 的“LimitRequestBody”,所以我向它添加了“LimitRequestBody 0”并重新启动了 Apache。那也没有帮助。

Any other suggestions???

还有什么建议???

回答by pix0r

This is probably due to your server's configuration. Check php.ini for the setting max_post_sizeand ensure that it is sufficiently large to post your data. Also check your web server settings - Apache has a LimitRequestBodydirective which could be causing your problem. Finally, check your web server and PHP error logs to see if the large post is triggering any errors.

这可能是由于您的服务器的配置。检查 php.ini 的设置max_post_size并确保它足够大以发布您的数据。还要检查您的 Web 服务器设置 - Apache 有一个LimitRequestBody指令可能会导致您的问题。最后,检查您的 Web 服务器和 PHP 错误日志,看看大帖子是否触发了任何错误。

回答by leighghunt

Just a couple of pointers to anyone else who finds this page - I ran into a similar issue with a JSON string failing to be parsed using jQuery.parseJSON().

只是指向找到此页面的其他人的几个指针 - 我遇到了类似的问题,无法使用 .json 解析 JSON 字符串jQuery.parseJSON()

I'm embarrassed to say my issue (probably unrelated to OP's issue) was actually caused by a stray single quote. Escaping it resolved the issue. I had orginally thought it was string length related, as it only seemed to be happening with an 8,000 character long JSON string, but it was the stray quote terminating the string in the wrong place.

我很尴尬地说我的问题(可能与 OP 的问题无关)实际上是由一个杂散的单引号引起的。逃避它解决了这个问题。我最初认为它与字符串长度有关,因为它似乎只发生在 8,000 个字符长的 JSON 字符串中,但它是在错误位置终止字符串的杂散引号。

Tim, I don't know if you ever got to the bottom of your original issue, but pasting the string you supplied:

蒂姆,我不知道你是否已经找到了原始问题的底部,但粘贴了你提供的字符串:

{"events":[{"dates":["2009-10-10","2009-10-11","2009-10-12"],"divisions":[{"level":"Collegiate","name":"Varsity","subdivision":"Division I","rounds":[],"teams":[{"id":"0","country":"USA","state":"CA","name":"California Polytechnic State University","subteam":""},{"id":"1","country":"USA","state":"CA","name":"California State University, Bakersfield","subteam":""},{"id":"2","country":"USA","state":"CA","name":"California State University, Fresno","subteam":""},{"id":"3","country":"USA","state":"CA","name":"California State University, Fullerton","subteam":""},{"id":"4","country":"USA","state":"CA","name":"Stanford University","subteam":""},{"id":"5","country":"USA","state":"CA","name":"University of California, Davis","subteam":""},{"id":"6","country":"USA","state":"CA","name":"San Francisco State University","subteam":""},{"id":"7","country":"USA","state":"CA","name":"Lassen Community College","subteam":""},{"id":"8","country":"USA","state":"CA","name":"Menlo College","subteam":""},{"id":"9","country":"USA","state":"CA","name":"Fresno Pacific University","subteam":""},{"id":"10","country":"USA","state":"CA","name":"Bakersfield","subteam":""},{"id":"11","country":"USA","state":"CA","name":"Buchanan","subteam":""},{"id":"12","country":"USA","state":"CA","name":"Campolindo-Moraga","subteam":""},{"id":"13","country":"USA","state":"CA","name":"Fremont-Sunnyvale","subteam":""},{"id":"14","country":"USA","state":"CA","name":"Ponderosa-Shingle Springs","subteam":""},{"id":"15","country":"USA","state":"CA","name":"West Covina","subteam":""},{"id":"16","country":"USA","state":"CA","name":"Gilroy","subteam":""},{"id":"17","country":"USA","state":"CA","name":"San José State University","subteam":""},{"id":"18","country":"USA","state":"CA","name":"University of California, Los Angeles","subteam":""},{"id":"19","country":"USA","state":"CA","name":"Sierra College","subteam":""},{"id":"20","country":"USA","state":"CA","name":"Selma","subteam":""},{"id":"21","country":"USA","state":"CA","name":"Liberty","subteam":""}],}]}]}

into http://json.parser.online.fr/gives the following error, if this is of any help to anyone else:

进入http://json.parser.online.fr/给出以下错误,如果这对其他人有帮助:

SyntaxError: JSON.parse: expected double-quoted property name

SyntaxError: JSON.parse: expected double-quoted property name

回答by Ronald Conco

It seems like there is a size issue, when testing my Json string everything works when the string is small ,when I increment the string (php array to be encoded) the output of the Json string gets chopped off.

似乎存在大小问题,当测试我的 Json 字符串时,当字符串很小时一切正常,当我增加字符串(要编码的 php 数组)时,Json 字符串的输出被切断。

when doing a string length on the failed string , I get (7796) . So I changed the max post option in the ini file both for the cli and apache to 64M instead of 8M and I am still getting the same problem. I do not think this problem is restricted to the apache LimitRequestBody since the php gives the same output on CLI.

在对失败的字符串执行字符串长度时,我得到 (7796) 。因此,我将 cli 和 apache 的 ini 文件中的 max post 选项更改为 64M 而不是 8M,但我仍然遇到同样的问题。我不认为这个问题仅限于 apache LimitRequestBody,因为 php 在 CLI 上给出了相同的输出。

One more thing, when doing a var_dump on the encoded json string, I can see when it gets chopped of and the json tags do not get closed, hence why the Json decoder return a null.

还有一件事,当对编码的 json 字符串执行 var_dump 时,我可以看到它何时被切掉并且 json 标签没有被关闭,因此为什么 Json 解码器返回空值。

e.g

例如

$strJson = file_get_contents('http://mydomain/page');
var_dump($strJson);

You will see where the string is being chopped off and a zero is concatenated to end of the output.

您将看到字符串被截断的位置,并且零连接到输出的末尾。

回答by intellitecture

'jsonp' datatype does not and can not use the POST method. Instead all data is passed in a query string. Thus, if the server's max query string length is exceeded, the data will not be passed. The reason for this is that jQuery loads the json data as a node into your html document, and this can only be done as a GET. If you need more data you'll have to use XML or other.

'jsonp' 数据类型没有也不能使用 POST 方法。相反,所有数据都在查询字符串中传递。因此,如果超过服务器的最大查询字符串长度,则不会传递数据。这样做的原因是 jQuery 将 json 数据作为节点加载到您的 html 文档中,而这只能作为 GET 来完成。如果您需要更多数据,则必须使用 XML 或其他。