使用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
});
效果很好。但是如果JSON字符串变得更大(不确定确切的大小,但大约大5倍),服务器收到的$ _POST数据为空。有谁知道为什么?
我正在使用Apache / PHP / jquery。它发生在IE和Firefox上。我需要调整配置设置吗?
以下是无法通过的字符串示例:
{"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":""}],}]}]}
它是使用json.org的“stringify”创建的:
var strJSON = JSON.stringify(oEvent);
修改 进一步调查,我将dataType更改为“text”并尝试发送一长串数字。它工作直到~3500个字符然后扼流圈(当我说“扼流圈”时,我的意思是请求到达服务器时带有$ _POST空。)
我的PHP post_max_size是64M,所以不是这样。有人建议使用Apache的“LimitRequestBody”,它不在httpd.conf中,所以我在其中添加了“LimitRequestBody 0”并重新启动了Apache。这也没有帮助。
还有其他建议???
答案 0 :(得分:6)
这可能是由于您的服务器配置造成的。检查php.ini中的设置max_post_size
并确保它足够大以发布您的数据。还要检查您的Web服务器设置--Apache有LimitRequestBody
指令可能导致您的问题。最后,检查您的Web服务器和PHP错误日志,以查看大帖子是否触发任何错误。
答案 1 :(得分:3)
只有几个指向找到此页面的其他人的指针 - 我遇到了类似的问题,JSON字符串无法使用jQuery.parseJSON()
进行解析。
我很尴尬地说我的问题(可能与OP的问题无关)实际上是由一个流浪的单引号引起的。逃避它解决了这个问题。我原本认为它与字符串长度有关,因为它似乎只发生了一个长度为8,000字符的JSON字符串,但它是在错误的地方终止字符串的流浪引用。
蒂姆,我不知道你是否曾触及原始问题的底部,但是粘贴了你提供的字符串:{"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":""}],}]}]}
进入http://json.parser.online.fr/会出现以下错误,如果这对任何其他人有任何帮助:
<强> SyntaxError: JSON.parse: expected double-quoted property name
强>
答案 2 :(得分:2)
似乎存在一个大小问题,当测试我的Json字符串时,当字符串很小时一切正常,当我增加字符串(要编码的php数组)时,Json字符串的输出会被切断。
在失败的字符串上执行字符串长度时,我得到(7796)。所以我将ini文件中的max post选项更改为cli和apache为64M而不是8M,我仍然遇到同样的问题。我不认为这个问题仅限于apache LimitRequestBody,因为php在CLI上提供了相同的输出。
还有一件事,当对编码的json字符串执行var_dump时,我可以看到它被切断并且json标记没有被关闭,因此Json解码器返回null的原因。
例如
$strJson = file_get_contents('http://mydomain/page');
var_dump($strJson);
您将看到字符串被切断的位置,并且零连接到输出的末尾。
答案 3 :(得分:1)
'jsonp'数据类型不能也不能使用POST方法。而是所有数据都在查询字符串中传递。因此,如果超出服务器的最大查询字符串长度,则不会传递数据。原因是jQuery将json数据作为节点加载到html文档中,这只能作为GET完成。如果您需要更多数据,则必须使用XML或其他。