Search code examples
javascriptasp.netgetrequestgzip

ajax send request with encoding gzip is not working


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);
}

Solution

  • 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