Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
876 views
in Technique[技术] by (71.8m points)

jquery - Gzipped JSON file not decompressing

I have large (30MB+) JSON files stored on my server as file.json. Using jQuery's getJSON("http://site/file.json") function it works just fine. But, as you'd probably think downloading 30MB worth of a JSON response takes a decent chuck of time.

Instead I'm now storing them as Gzip'd files (file.json.gz) which cuts them down to just under 1MB! I'd like to do the same thing by using getJSON("http://site/file.json.gz"), but it looks like browsers are not decompressing the GZIP'd response, so parsing it doesn't work.

So, is there any way to get jQuery or the browser to decompress a static JSON file that's been compressed using GZIP such as file.json.gz?

BTW: The saved files are compressed properly. If I manually decompress them I get the 30MB+ valid JSON file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Browsers don't automatically decompress just any compressed data they run across. The server has to tell the browser that the stream is only compressed for transport and it needs to be decompressed before processing (otherwise you wouldn't be able to download and save compressed archives at all!) You need to make your web server send the appropriate headers (Content-Encoding), but do check that the browser supports compression in the first place before doing that (the Accept-Encoding header). Most webservers can also gzip things for you on the fly, unless that's too big of a performance hit.

Somehow I have the feeling you're going to have more trouble parsing that much JSON on the browser than retrieving it...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...