No auto-decompression of gzipped json on browser's side when using angular's http get method

牧云@^-^@ 提交于 2020-01-13 05:13:08

问题


I'm trying to load a json file using angular (v1.2.6):

$http.get('myfile.json').success(function(data) { ... }

This works fine, except when I create a (static) compressed version of the file on the server, and try to load 'myfile.json.gz' instead (to reduce the loading time).

The request headers seem correct (Chrome 31.0 on Mac) (as stated here and here):

Accept: application/json, text/plain, */*
Accept-Encoding: gzip,deflate,sdch

while the response headers contain:

Connection: close
Accept-Ranges: bytes
Content-Length: 702468
Content-Type: application/x-gzip
Content-Encoding: gzip

However, the content is not automatically decompressed by the client browser, as I understand it should be. data.length is ~700Kb instead of the original uncompressed ~3Mb.

Although this one post suggests it needs to be done manually, I understand decompression should be automatic and transparent.

My question is, should it be decompressed automatically? and why isn't it the case here?


回答1:


Your content type should not be "application/x-gzip* it should stay : application/json

The content encoding is enough to say to the browser that the content is zipped.

What HTTP server are you running ? You should configure it in order to return the correct mime type, regardless of the .gz extension.



来源:https://stackoverflow.com/questions/20859246/no-auto-decompression-of-gzipped-json-on-browsers-side-when-using-angulars-htt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!