3

Ajax send request with encoding gzip (iis7) is not working below are the code for send request can some one help me what is wrong in my code.

Thanks in advance

function sendRequest(url, callback, postData)
{
   var req = createXMLHTTPObject();
   if (!req) {
      return;
   }

   var method = (postData) ? "POST" : "GET";
   req.open(method, "xml/" + url, true);
   req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');

   if (postData) {
      req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      req.setRequestHeader("Content-Encoding", "gzip");

   }

   req.onreadystatechange = function() {

   }

   req.send(postData);
}
Alberto
  • 718
  • 4
  • 20
MANISHDAN LANGA
  • 2,227
  • 6
  • 29
  • 43
  • 1
    What do you mean by not working? Is the server not responding as it should? Do you get an error? – Nadh Apr 27 '12 at 06:48
  • 1
    Are you really compressing the content or just pretending it is compressed by changing the header ? Is postData gzipped ? – Denys Séguret Apr 27 '12 at 07:04
  • @dystroy yes, its compress problem for download xml file with size is large – MANISHDAN LANGA Apr 27 '12 at 07:18
  • @NADH not getting error when download xml file using this code.if xml file is 6 mb then its download for 400kb then stop (as i see in Firebug). if i remove gzip from IIS the its download properly with 6mb. – MANISHDAN LANGA Apr 27 '12 at 07:22

3 Answers3

5

Considering the security, browser does not allow you to override some headers including "Content-Encoding".

tuoxie007
  • 1,234
  • 12
  • 10
2

One way to transparently have the requests for your XMLHttpRequest highly compressed is to use HTTP/2 (e.g. serve your website via CloudFlare).

When using HTTP/2, then although the HTTP headers do not say Content-Encoding: gzip the underlying HTTP/2 protocol compresses everything.

It also compresses much better than gzip because:

  • it compresses headers
  • header compression uses a standard dictionary
  • I think data compression builds a dictionary over multiple messages (brotli - I haven't double-checked that though)

You can see if your server is using HTTP/2 by:

  1. Open Chrome, and F12 to open developer tools
  2. Click on the network tab
  3. close the request inspector panel (has tabs Headers Preview Response Timing)
  4. Right click on the Name header of the list of requests and tick Protocol
  5. Navigate to your website and watch what protocol is used for all requests - in the protocol column you want to see h2 not http/1.1

Example log of HTTP/2 network requests withn Chrome developer tools

I wouldn't recommend using JavaScript compression libraries because that causes slowdown and inefficiencies.

robocat
  • 5,293
  • 48
  • 65
1

The problem doesn't seem to be related to header but to compression.

You don't seem to compress your postData.

If postData is already compressed, no need to try to manually set content-encoding.

If it is not, either let the browser negotiate the transfer encoding with the server (this is part of the protocol and done automatically, the server saying if it accepts it, but I think that's rarely the case) or (if you really really need to) encode it yourself. This SO question indicates a library to compress browserside : JavaScript implementation of Gzip

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    Why does the programmer need to implement gzip for data transfer? There must be a way for the browsers to auto compress natively. I don't want my clients to upload 10MB of uncompressed json payload, that is common sense. – Phil Jan 27 '17 at 14:03