views:

1406

answers:

4

I'm trying to narrow down some weirdness going on with my AJAX calls. My PHP scripts have this at the start:

ob_start("ob_gzhandler");

Works great with HTML. But are there any problems with doing it with application/json data? Any browser issues anyone is aware of?

+3  A: 

I don't think so... I've used static files stored as gzipped JSON before, and it worked fine with AJAX.

edit: I checked my php script and the only thing special I did was to include these headers:

Content-Encoding: gzip
Content-Type: text/plain

If I remember right, whenever I tried to change the Content-Type to something that would indicate JSON, the client had trouble.

Jason S
Thats interesting. I use application/json cos I thought thats what you should do for json.
Jordie
I'm trying to remember the exact issue I had. It may have been that the AJAX part didn't mind, but for debugging it was a big pain because Firefox doesn't know how to display application/JSON, whereas it works fine with text/plain.
Jason S
(by "debugging" I mean viewing the JSON url directly in my browser, rather than by putting breakpoints/logging in the AJAX part of my application)
Jason S
+1  A: 

You can give out gzipped content whenever the browser specifies gzip in Accept-Encoding request header. In that case, there is no difference between JSON and HTML and no problems will be caused whatsoever.

Mehrdad Afshari
+2  A: 

Some older browsers, like certain versions of IE6, screw up gzipped content, especially js content.

Just check that your server sends proper content-encoding header, that is

Content-Encoding: gzip

You should also check the headers sent by the browser for proper accept-encoding header before sending gzipped content... that is,

Accept-Encoding: gzip,deflate
kkyy
But this is what ob_gzhandler is doing right?
Jordie
That's the idea with ob_gzhandler
Jayrox
A: 

Instead of enabling compression in PHP, I would enable compression in Apache (using mod_deflate) so that you can check for various incompatible browsers and only send compressed data for the browsers that accept it and handle it correctly.

http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

Jordan Ryan Moore
Thats what ob_gzhandler does. I prefer programmatic control.
Jordie