Solved

How to gzip JSON from PHP using AJAX?

Posted on 2013-12-09
10
2,200 Views
Last Modified: 2013-12-11
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

0
Comment
Question by:Victor Kimura
  • 4
  • 3
  • 3
10 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
If you are talking about delivering your JSON in 'gzip' format to the browser, that is not something that you choose or do.  That is a negotiation between the browser and the web server.

In the HTTP request from the browser, the browser tells the server what formats it can accept in the response.  In the HTTP response (when 'gzip' is acceptable), the server checks the file size to determine whether it would be quicker to just send a small file or gzip and send a larger file and then does what seems to be the best choice.
0
 

Author Comment

by:Victor Kimura
Comment Utility
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:
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_28313939.html
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
The 'gzip' method of sending data from the server to the browser is the same for all file types.  You can 'gzip' a file for storage on your server but that is a different thing.
0
 

Author Comment

by:Victor Kimura
Comment Utility
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.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
If the JSON is so large that making it smaller is a worthwhile objective, the application is misdesigned.  JSON_UNESCAPED_UNICODE is not related to GZIP.  PHP says: "Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0."

The JSON standard requires UTF-8.  PHP has a long history of thinking that a byte == a character, and this is not true in UTF-8, where characters can be made up from multiple bytes.  So my guess is that PHP was encoding the Unicode characters to try to play nice with legacy applications and avoid breaking the JSON standard.

See these links for more info on Unicode:
http://php.net/manual/en/json.constants.php
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Victor Kimura
Comment Utility
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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
72KB is not too large, nor is ten times that.  I was thinking more like the size of the Amazon.com catalog as being too large.

If you're finding timeouts when you submit the form, you might consider a design that used AJAX to submit the form in parts.
0
 

Author Closing Comment

by:Victor Kimura
Comment Utility
Thanks so much for your valuable input. Both of you! =)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Glad we could help.  Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now