Create a gist now

Instantly share code, notes, and snippets.

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

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

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

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