强制浏览器将 XHR 响应解释为 gzip
Force browser to interpret XHR response as gzip
我正在编写一个 client-only JavaScript 应用程序,它下载(通过 XHR)相对较大的 JSON 数据集以供显示。我想提前 gzip 这些数据集以节省 space 和带宽(例如 mydata.json.gz
)。
不幸的是,我无法控制服务器配置,它为我的 .json.gz
文件发送 Content-Type: application/x-gzip
headers,而不是正确的 Content-Encoding: gzip
和 Content-Type: application/json
。因此,我的应用程序接收到原始 gzip 压缩数据,而不是我可以传递给 JSON.parse()
.
的数据
我知道一些 pure-JavaScript Gzip 实现(例如 pako)大概可以完成工作,但是有什么方法可以欺骗浏览器为我解码它不需要一个不必要的 45KB 库吗?
I'm aware of a few pure-JavaScript Gzip implementations (e.g. pako) that could presumably get the job done
使用其中之一
but is there any way to trick the browser into decoding it for me that won't require an otherwise unnecessary 45KB library?
没有
服务器说 "Here is a gzip file" 而不是 "Here is a JSON file that is being transmitted using a gzip encoding"。
您必须更改 HTTP 响应 headers 才能让浏览器对其进行透明解码。
我正在编写一个 client-only JavaScript 应用程序,它下载(通过 XHR)相对较大的 JSON 数据集以供显示。我想提前 gzip 这些数据集以节省 space 和带宽(例如 mydata.json.gz
)。
不幸的是,我无法控制服务器配置,它为我的 .json.gz
文件发送 Content-Type: application/x-gzip
headers,而不是正确的 Content-Encoding: gzip
和 Content-Type: application/json
。因此,我的应用程序接收到原始 gzip 压缩数据,而不是我可以传递给 JSON.parse()
.
我知道一些 pure-JavaScript Gzip 实现(例如 pako)大概可以完成工作,但是有什么方法可以欺骗浏览器为我解码它不需要一个不必要的 45KB 库吗?
I'm aware of a few pure-JavaScript Gzip implementations (e.g. pako) that could presumably get the job done
使用其中之一
but is there any way to trick the browser into decoding it for me that won't require an otherwise unnecessary 45KB library?
没有
服务器说 "Here is a gzip file" 而不是 "Here is a JSON file that is being transmitted using a gzip encoding"。
您必须更改 HTTP 响应 headers 才能让浏览器对其进行透明解码。