views:

46

answers:

4

Hi,

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/request.

What would be the simplest way to do this?

All communications is done via JSON strings (both request and response).

+1  A: 

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%

Marcos Placona
+2  A: 

The simplest way to do this is turn http compression on on your server and let IIS and the browser figure it out.

Chris Lively
i want to do this explicitally
Theofanis Pantelides
if possible that is
Theofanis Pantelides
Why? The built in compression is free, will work with all of your pages, is fast, and ultimately will be much better than handling it yourself.
Chris Lively
because there are multiple servers in the farm which keep being changed around, and i do not have access to them, so i prefer to be independant of the infrastructure (if possible)
Theofanis Pantelides
It sounds like your infrastructure team needs some documentation on your web server config requirements. Ask them what it will take to get things set up correctly, and keep them that way. This is no different than if you had other special requirements.
Chris Lively
+2  A: 

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.

Marc Gravell
+1  A: 

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

Pierre-Loic Doulcet