Ajax封装

<!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" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>jsonp</title>
</head>
<body>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<!-- <script type="text/javascript" src="jquery-1.11.1.js"></script> -->
<script type="text/javascript">
/*Ajax =
function(){
    function request(url,opt){
        function fn(){}
        var async   = opt.async !== false,
            method  = opt.method    || ‘GET‘,
            data    = opt.data      || null,
            success = opt.success   || fn,
            failure = opt.failure   || fn;
            method  = method.toUpperCase();
        if(method == ‘GET‘ && data){
            url += (url.indexOf(‘?‘) == -1 ? ‘?‘ : ‘&‘) + data;
            data = null;
        }
        var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject(‘Microsoft.XMLHTTP‘);
        xhr.onreadystatechange = function(){
            _onStateChange(xhr,success,failure);
        };
        xhr.open(method,url,async);
        if(method == ‘POST‘){
            xhr.setRequestHeader(‘Content-type‘, ‘application/x-www-form-urlencoded;‘);
        }
        xhr.send(data);
        return xhr;
    }
    function _onStateChange(xhr,success,failure){
        if(xhr.readyState == 4){
            var s = xhr.status;
            if(s>= 200 && s < 300){
                success(xhr);
            }else{
                failure(xhr);
            }
        }else{}
    }
    return {request:request};  
}();*/
</script>
<script type="text/javascript">
(function(root){
    if(typeof(Ajax)=="undefined"&&!Ajax) var Ajax={};
    Ajax.request=function(obj){
        var xhr=new Ajax.creat();
        return xhr.request(obj);
    }
    Ajax.creat=function(){
        this.ajax=this.get();
    }
    Ajax.creat.prototype={
        get:function(){
            try{return new XMLHttpRequest();}catch(e){}
            try{return new ActiveXObject(Msxml2.XMLHTTP.6.0);}catch(e){}
            try{return new ActiveXObject(Msxml2.XMLHTTP.4.0);}catch(e){}
            try{return new ActiveXObject(Msxml2.XMLHTTP.3.0);}catch(e){}
            try{return new ActiveXObject(Msxml2.XMLHTTP);}catch(e){}
            try{return new ActiveXObject(MSXML3.XMLHTTP);}catch(e){}
            try{return new ActiveXObject(MSXML.XMLHTTP);}catch(e){}
            try{return new ActiveXObject(Microsoft.XMLHTTP);}catch(e){}
            try{return new ActiveXObject(MSXML2.ServerXMLHTTP);}catch(e){}
            return null;
        },
        request:function(obj){
            var self=this,ajax=self.ajax;
            if(typeof(obj)!=="object"||ajax==null) return;
            var url=obj.url,
                type=obj.type||"GET",
                data=obj.data||null,
                async=typeof(obj.async)=="boolean"? obj.async : true,
                success=obj.success||new Function(),
                error=obj.error||new Function();

            if(typeof(data)=="object") data=self.objectToStr(data);
            
            if(type=="GET"&&data) url+= (url.indexOf("?")==-1?"?":"&")+data;

            ajax.open(type,url,async);

            if(type=="POST") ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded;");

            ajax.send(data);

            ajax.onreadystatechange=function(){
                self.handle(ajax,success,error);
            }
            return ajax;
        },
        objectToStr:function(s){
            var str="";
            for(var x in s){
                str+= x+"="+s[x]+"&";
            }
            str=str.substring(0,str.lastIndexOf("&"));
            return str;
        },
        handle:function(xhr,success,error){
            if(xhr.readyState==4){
                var status=xhr.status;
                if(status>=200&&status<=300) success(xhr);
                    else error(xhr);
            }else{}
        }
    }
    root.ajax={request:Ajax.request}
})(window)
//url,type,data,async,success,error
</script>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。