简体   繁体   English

如何在javascript中解压ajax接收到的gzip数据?

[英]How to decompress the gzip data received by ajax in javascript?

Hellow!你好!

There is a problem: php sends data compressed with gzdeflate():有一个问题:php发送的数据是用gzdeflate()压缩的:

$string=gzdeflate($string,9);
echo $string;

In the browser pako.js is included and the following runs:在浏览器中包含 pako.js 并运行以下内容:

var rsp=rst.responseText;
rsp=pako.inflate(rsp);

But the console says: "Uncaught unknown compression method".但是控制台说:“未捕获的未知压缩方法”。

(The ajax code works well without compression and pako.inflate.) (ajax 代码在没有压缩和 pako.inflate 的情况下运行良好。)

If I use base64_encode()如果我使用 base64_encode()

$string=base64_encode($string);

or use gzdeflate() instead of gzencode(), I have the same problem.或使用 gzdeflate() 而不是 gzencode(),我有同样的问题。

How to unpack the gzip data in javascript?如何在javascript中解压gzip数据?

Thank you in advance.先感谢您。

I'm not familiar with php, so I kinda struggled with this problem even after reading the comments here, so I thought to post a minimal working solution in php:我对 php 不熟悉,所以即使在阅读了这里的评论后,我还是有点挣扎于这个问题,所以我想在 php 中发布一个最小的工作解决方案:

$response = gzdeflate('My data', 9, ZLIB_ENCODING_DEFLATE);

header('Content-Encoding: deflate');
echo $response;

No need to use pako then, the data will be decompressed by the browser.不需要使用pako,数据会被浏览器解压。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM