doutangguan2460 2019-02-11 13:40
浏览 107
已采纳

在PHP中编码数据并在Javascript(ajax)中解压缩

In order to reduce the size of my json file, I wanted to zip it. Hence, I am trying to compress json data file sent from php server :

header('Content-Encoding: gzip'); 
$output = gzencode(json_encode($data));     
echo $output;

And then, uncompress it in javascript (ajax methodd) :

$.ajax({
    url: url,
    type: "GET",
    headers : {'Accept-Encoding': 'gzip '},
    async: true,
    success: function (data) {
        console.log("sucess !!");
        console.log(data);
        ... 
    )};

I also modified apache server in order to uncompress it automagically :

LoadModule deflate_module modules/mod_deflate.so

And :

 <IfModule mod_headers.c>
  <FilesMatch ".(js|css|xml|gz|txt|json)$">
      Header append Vary: Accept-Encoding
  </FilesMatch>

However, I couldnt get the data properly (it stills compress).

Could you please let me know what I am doing wrong ?

Thank you

  • 写回答

1条回答

  • douxiandiyo58855 2019-02-11 14:14
    关注

    To use gzip for the API there's no need to implement it on the application level (compress data in PHP and decompress it in JS). In this case the data should be compressed by the web server (Apache) and decompressed by the client (browser). This process is transparent and independent from the PHP or JS code.

    Make sure that the web server is configured to use compression with your API endpoints. To test it run curl -I -H 'Accept-Encoding: gzip,deflate' https://your.domain/api/endpoint/name or inspect the request in your browser devtools (under Network tab).

    If there's a content-encoding: gzip header in the response, it means that the server uses gzip for compression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作