Created
August 17, 2012 07:10
-
-
Save composite/3376653 to your computer and use it in GitHub Desktop.
My first tiny ajax snippet (test)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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