Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created September 29, 2010 12:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/602694 to your computer and use it in GitHub Desktop.
Save thinkphp/602694 to your computer and use it in GitHub Desktop.
AJAX
if(typeof asyncRequest == 'undefined') {
asyncRequest = {};
}
asyncRequest.REQUEST = function() {
function handleReadyState(o,callback) {
o.onreadystatechange = function() {
if(o.readyState == 4) {
if(o.status == 200) {
callback(o.responseXML);
}
}
}
}
var XHR = function() {
var http;
try {
http = new XMLHttpRequest();
XHR = function(){return new XMLHttpRequest();}
}catch(e) {
try {
http = new ActiveXObject("Microsoft.XMLHTTP");
XHR = function(){return new ActiveXObject("Microsoft.XMLHTTP");}
}catch(e){}
}
return XHR();
}
return function(method,url,callback,postData) {
var http = XHR();
http.open(method,url,true);
if(postData) {
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", postData.length);
http.setRequestHeader("Connection", "close");
}
handleReadyState(http,callback);
http.send(postData || null);
return http;
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment