完美相容IE、FF、Opera的Ajax類支援get、post、自訂回呼函數
來源:互聯網
上載者:User
function Ajax(){ this.httpXML=false; this.init(); } Ajax.prototype.init=function(){ if(window.XMLHttpRequest){ this.httpXML=new XMLHttpRequest(); if (this.httpXML.overrideMimeType){ this.httpXML.overrideMimeType("text/xml"); } }else if(window.ActiveXObject){ try{this.httpXML = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){ try{this.httpXML = new ActiveXObject("Microsoft.XMLHTTP");}catch (e){} } } if(!this.httpXML){ window.alert("can't create XMLHttpRequest object"); return null; } } Ajax.prototype.send=function(_method,_url,_data,_callback){ this.httpXML.onreadystatechange=function(){ if(this.readyState==4&&this.status==200){ _callback.call(this,this.responseText); } } if (_method.toLowerCase() == "get"){ this.httpXML.open(_method, _url, true); this.httpXML.send(null); }else if (_method.toLowerCase() == "post"){ this.httpXML.open(_method, _url, true); this.httpXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); this.httpXML.send(_data); } }//===========================================function getResult(data){alert(data);}new Ajax().send('post','url','a=1&b=2',getResult);