Reputation: 343
my first post here :)
I got json file 'my_data.json'. It's about 3 MB. I load this with:
$(document).ready(function() {
$.ajax({
'url' : 'my_data.json',
dataType: 'json',
success: function(json){
and everything is OK, script gets data from file and I can build a list from them.
Because the size od the file I want to use compression, but my hosting provider has disabled mod_gzip and mod_deflate. So I compressed the file using PHP gzencode and now the file is 250kB only. New file is 'my_data.json.gz'.
When I replace "my_data.json" with "my_data.json.gz" in previous code, nothing happends. I understand that I should inform the browser about the data are compressed. I read about headers and tried to add:
beforeSend: function (xhr){
xhr.setRequestHeader("Content-Encoding","gzip");
},
or
headers: {"Content-Encoding": "gzip"},
after the line
dataType: 'json',
but nothing happends.
What I'm doing wrong? In what file should I set and what?
Please, point me into right direction.
Upvotes: 0
Views: 2121
Reputation: 343
I got answer from my hosting provider and it works perfectly. For anyone who needs this: .htaccess file should be like this:
AddEncoding gzip gz
<FilesMatch "\.gz$">
ForceType text/plain
Header set Content-Encoding: gzip
</FilesMatch>
That's all.
But if you want to use PHP to send the file instead of .htaccess changing, create new file 'senddata.php' and put this in:
<?php $file = 'testowe-dane.json.gz';
if (file_exists($file))
{ header( "Content-Encoding: gzip" );
ob_clean();
flush();
readfile($file);
exit; }
?>
then load data from this php file:
$.ajax({ 'url' : 'senddata.php', dataType: 'json', success: function(json)
... and so on
Upvotes: 1