JSON object max size?
Solution 1
This is probably due to your server's configuration. Check php.ini for the setting max_post_size
and ensure that it is sufficiently large to post your data. Also check your web server settings - Apache has a LimitRequestBody
directive 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.
Solution 2
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()
.
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.
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:
SyntaxError: JSON.parse: expected double-quoted property name
Solution 3
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.
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.
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.
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.
Solution 4
'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.
Admin
Updated on October 27, 2020Comments
-
Admin over 3 years
Using jquery, I'm able to send json objects from client to server using ajax like this:
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?
I'm using Apache/PHP/jquery. It happens from both IE and Firefox. Is there a config setting somewhere I need to adjust?
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":
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.)
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.
Any other suggestions???
-
rfunduk over 14 yearsCan you try and have it not a string to start? As in, just have a JavaScript object that you pass to the
data
parameter of theajax
call. See what happens. -
Asaph over 14 yearsPlease post an example of failing JSON. 5x bigger than what you have posted is not really that big. It might be an escaping problem or structure problem in your JSON string.
-
Admin over 14 yearsAlright, I just added an example above. It's created with stringify().
-
rfunduk over 14 yearsNo no I mean literally pass the object to
data:
rather than using any sort of stringify at all. Pass jQuery the Object, it will turn it into JSON. If that still fails then it's probably what pix0r said, server configuration issue. -
Crescent Fresh over 14 years@thenduks: jQuery does not serialize anything to JSON. If you pass
.ajax
an object you get the standardfoo=1&bar=2
request body/query generated for you. If the OP wants JSON thenJSON.stringify
is the way to go. -
Admin over 14 yearsI tested it out, and it's not what thenduks suggested. If I do that, I receieve a JSON object (instead of a string) in $_POST, and I don't think PHP knows what to do with that. (PHP's json_decode() needs a string passed to it.) Anyways, it after further review, it isn't a JSON issue at all. If I change dataType to 'text' and send just a long string of text, it works only up to about 3500 chaacters (see my new comments above).
-
Blaise over 10 yearsso how is this problem fixed?
-
Vladimir Ch about 7 yearsmb increase max_input_vars + check for other limitations.serverOS/browser e t.c.
-
-
Admin over 14 yearsI thought that too, but this is what it reads: post_max_size = 64M That's 64 megabytes, correct?
-
pix0r over 14 yearsAlso check httpd.conf for
LimitRequestBody
-
pix0r over 14 years(and yes, 64M == 64 megabytes).
-
Admin over 14 yearsI set "LimitRequestBody 0" (unlimited) in httpd.conf and rebooted Apache. It had no effect. Any other ideas?
-
solefald almost 14 yearsRonald, were you ever able to find a solution to this? I am facing exact same issue, though my string gets cut off at 3127 characters
-
intellitecture over 12 yearsShould have specified (thought I did): The json request is sent by adding a <script> node to your html doc. All data is passes in a query string. Ex.: <script src="script.js?data1=example&data2=... There is no way to send POST data in a <script> tag. Ergo, the size of the data is limited by the server's maximum size for query strings / URLs.
-
Rup about 12 yearsAn anonymous user comments: There is a trailing comma after the array which is likely causing this error:
..."Liberty","subteam":""}],}]}]}
This is incorrect JSON. Some browsers can recover by ignoring trailing commas, but some cannot parse it. -
Vladimir Ch about 7 yearsand probably limit of of your browser =)