Solved

How to gzip JSON from PHP using AJAX?

Posted on 2013-12-09
10
2,368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706636
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
ID: 39706662
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706688
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Victor Kimura
ID: 39706746
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39708321
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
 

Author Comment

by:Victor Kimura
ID: 39709351
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 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39709399
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
ID: 39711899
Thanks so much for your valuable input. Both of you! =)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39711959
Glad we could help.  Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39712076
You're welcome, glad to help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to customize the text in the legend in highcharts 3 32
how can i break an array into a couple 5 24
Echo'd values in dropdowns 6 29
Find RGB colors from a screen. 2 17
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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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)

749 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