`
xiegangthrille
  • 浏览: 92060 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

完美兼容IE、FF、Opera的Ajax类支持get、post、自定义回调函数

阅读更多
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"){ 
         //if(navigator.userAgent.indexOf("MSIE")<=0){
         //     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
         //}
         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); 
	} 
	
}

Ajax.prototype.callback=function(){
	 
}

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>完美兼容IE、FF、Opera浏览器</title>
    <script type="text/javascript" src="js/ajax.js"></script>
    <script type="text/javascript">
        function send(){
            ajax=new Ajax();
            ajax.send("get","listener/Hajax.ashx","post测试请求",ddd);
        }
        
        function ddd(data){
            alert(data);
        }
    </script>
</head>
<body>
    <button onclick="send()">测试</button>
</body>
</html>

 

分享到:
评论
1 楼 luogen33 2012-02-10  
  

相关推荐

Global site tag (gtag.js) - Google Analytics