Skip to content

Instantly share code, notes, and snippets.

@datchley
Created June 3, 2014 19:58
Show Gist options
  • Select an option

  • Save datchley/c105ba5408e30fbcee10 to your computer and use it in GitHub Desktop.

Select an option

Save datchley/c105ba5408e30fbcee10 to your computer and use it in GitHub Desktop.
Plain javascript XMLHttpRequest implementation.
function xhr(){
try {
return new XMLHttpRequest();
}catch(e){}
try {
return new ActiveXObject("Msxml3.XMLHTTP");
}catch(e){}
try {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
}catch(e){}
try {
return new ActiveXObject("Msxml2.XMLHTTP.3.0");
}catch(e){}
try {
return new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){}
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
return null;
}
function ajaxRequest(url, callback, postData) {
var req = xhr();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method, url, true);
// req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment