使用Ajax请求gzip压缩数据 [英] Using ajax to request gzip compressed data
问题描述
我正在对使用gzip压缩内容进行响应的端点进行$ .ajax调用.为了我的一生,我无法弄清楚如何获得它,以便浏览器以这种方式看到它并可以解压缩它.我正在提出的Ajax请求是
I'm making an $.ajax call to an endpoint that is responding with gzipped content. For the life of me I cannot figure out how to get it so that the browser sees it that way and can uncompress it. The ajax request I'm making is
$.ajax({
url: 'http://my.server/compressed.html',
method: 'post',
beforeSend: function (request) {
//request.setRequestHeader("Accept-Encoding", "compress, gzip");
},
success: function (data) {
console.log('success', data);
},
error: function ( jqXHR, textStatus, errorThrown ) {
console.log('error', textStatus, errorThrown);
}
});
触发成功功能时,数据仅显示为
When the success function is triggered, data is just showing as
���1 �PDѽ�:ޏ�$�+A,D����*��w%���T�9�g���1G����h�6��'�nۖ\����~�%^
如您所见,我已经尝试设置'Accept-Encoding'头,但是这样做的错误是拒绝设置不安全的标头"Accept-Encoding"
.我已尝试按照此答案 PHP手册GZip编码所述在服务器端设置标头但这也无济于事.我只是不知道我在想什么.我需要在$ .ajax()请求(可能还有服务器响应)中包括什么才能使它获得成功,以便成功回调具有未压缩的数据?
As you can see I've tried setting the 'Accept-Encoding' head but the error I'm getting when I do that is Refused to set unsafe header "Accept-Encoding"
. I've tried setting headers on the server side as described in this answer PHP Manual GZip Encoding
but that didn't help me either. I just don't know what I'm missing. What do I need to include in the $.ajax() request (and possibly the server response) to get it so that the success callback has the uncompressed data?
thnx,
克里斯多夫(Christoph)
thnx,
Christoph
推荐答案
derp.我完全错过了(并且以为有了)
derp. I totally missed (and thought I had) the header
header('Content-Encoding: gzip');
将其添加到中提琴中,即可使用.有时候我不聪明...
Adding that and viola, it works. I'm not bright sometimes...
thnx,
克里斯多夫(Christoph)
thnx,
Christoph
这篇关于使用Ajax请求gzip压缩数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!