I want to shrink the data that is sent back and forth via AJAX, so that it sends/receives less data.
I will de/compress server-side (C#) and client-side (Javascript), before/after every response/r开发者_运维技巧equest.
What would be the simplest way to do this?
All communications is done via JSON strings (both request and response).
The simplest way to do this is turn http compression on on your server and let IIS and the browser figure it out.
The simplest thing here would be to simply enable http compression of your existing JSON.
You could possibly get more aggressive with it though - for example, I haven't played with it but a few weeks ago somebody mentioned a javascript protocol buffers implementation. Have your server return some raw bytes (or maybe base-64 or hex) and you could see some significant savings. It would involve changing your code quite a bit, though.
Caveat: although I'm pretty familiar with protobufs generally, I have not used that javascript implementation. YMMV.
I would not advise you to have too many data sent via JSON, but if it's really necessary, I'd suggest you GZIP your data.
There's a nice article about it here: http://www.west-wind.com/Weblog/posts/10564.aspx
With data GZIPED, you can save up to 90%
1
You can set "Content-encoding" to "gzip" in server side to encode the HTTP flux:
Response.AppendHeader("Content-encoding", "gzip");
2
You can manually Zip your JSON in client and server side :
For client side you can use a library like : http://jszip.stuartk.co.uk/
For server size you can use .Net framework to compress / decompress
精彩评论