Skip to content

Instantly share code, notes, and snippets.

@composite
Created August 17, 2012 07:10
Show Gist options
  • Save composite/3376653 to your computer and use it in GitHub Desktop.
Save composite/3376653 to your computer and use it in GitHub Desktop.
My first tiny ajax snippet (test)
function getAjax(url,params,method){
var xhr = XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject && new ActiveXObject("Microsoft.XMLHTTP")) || null;
var _deff = function(){this.fn={};};
_deff.prototype.text=function(fn){this.fn.text=fn;}
var get = new _deff();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4) {
if('text' in get.fn&&typeof(get.fn.text)=='function') get.fn.text.call(xhr,xhr.responseText);
}
};
xhr.open(method || 'GET', url || '', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
if(typeof(params)=='string') xhr.setRequestHeader('Content-length', params.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(params);
return get;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment