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

Viewed 389

Hellow!

There is a problem: php sends data compressed with gzdeflate():

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

In the browser pako.js is included and the following runs:

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.)

If I use base64_encode()

$string=base64_encode($string);

or use gzdeflate() instead of gzencode(), I have the same problem.

How to unpack the gzip data in javascript?

Thank you in advance.

1 Answers

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:

$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.

Related