views:

1104

answers:

2

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???

+4  A: 

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.

pix0r
I thought that too, but this is what it reads: post_max_size = 64MThat's 64 megabytes, correct?
Also check httpd.conf for `LimitRequestBody`
pix0r
(and yes, 64M == 64 megabytes).
pix0r
I set "LimitRequestBody 0" (unlimited) in httpd.conf and rebooted Apache. It had no effect. Any other ideas?
A: 

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.

Ronald Conco
Ronald, 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
solefald