Skip to content

Instantly share code, notes, and snippets.

@mamund
Created August 27, 2012 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamund/3490606 to your computer and use it in GitHub Desktop.
Save mamund/3490606 to your computer and use it in GitHub Desktop.
XmlHttpRequest w/ compression support
// typical generic request routine (handles GET and POST)
function makeRequest(href, next, body, contentType) {
var ajax;
ajax=new XMLHttpRequest();
if(ajax) {
ajax.onreadystatechange = next();
if(body) {
ajax.open('post',href,false);
ajax.setRequestHeaders('content-type',contentType);
ajax.setRequestHeaders('accept-encoding','gzip');
ajax.send(body);
}
else {
ajax.open('get',href,false);
ajax.setRequestHeaders('accept-encoding','gzip');
ajax.send(null);
}
}
}
@shmert
Copy link

shmert commented Jul 23, 2014

Thanks Mamund, but it should be:

ajax.setRequestHeader('accept-encoding','gzip');

Also, I get this error from Chrome:

Refused to set unsafe header "accept-encoding"

@ApoorvSaxena
Copy link

Using Content-Encoding: gzip in the request headers does not work. Discussed here: http://stackoverflow.com/a/10346303/362271

@guyoun
Copy link

guyoun commented Nov 8, 2016

Accept-Encoding is one of forbidden header name.
See https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment