Link to home
Start Free TrialLog in
Avatar of Victor Kimura
Victor KimuraFlag for Canada

asked on

How to gzip JSON from PHP using AJAX?

Hi,

I'm wondering how I can gzip json data from PHP with an AJAX call.

I was reading here that in PHP 5.4 you can use:

json_encode($data,JSON_UNESCAPED_UNICODE);

Unfortunately, I don't have PHP 5.4 installed right now.

So the other user suggested:

ob_start('ob_gzhandler');

But that just doesn't work when I place that code at the top of my PHP file. Server returns error.

I'm looking for an alternative to:
json_encode($data,JSON_UNESCAPED_UNICODE);

and also how would I unzip the json data in the $.ajax call:

$.ajax({
    type : 'post',
    url: './model/06_modCheckPrimaryBeneficiaries' + sAppendDateTime + '.php',
    cache: false,
    dataType: 'json',
    success: function(retData, textStatus, jqXHR) {
        var test = retData;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('textStatus: '+textStatus);
    });

Open in new window

SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor Kimura

ASKER

Oh, okay.

So, this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

isn't really gzipping it then?

Is that true of some .js files too? I have another post here that you may be able to answer too:
https://www.experts-exchange.com/questions/28313939/gzip-js-not-displaying-in-headers.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, thanks.

just wondering if you know if this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

will make the json data smaller or more efficient then just regular encoding.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I see. How big is too big? I have a some form fields that return in JSON format. The user can add several beneficiaries (it's for estate planning) to this particular page and each beneficiary the user adds increases the JSON by about 24 KB. So, if the user adds in 3 beneficiaries, then that JSON return is about 72 KB. Is that too large? I just want to see how I can optimize the JSON and what's considered too large. I'm trying to see how I can make this more efficient.

Thank you for your input.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much for your valuable input. Both of you! =)
Glad we could help.  Thanks for the points and thanks for using EE, ~Ray
You're welcome, glad to help.