首页 > 代码库 > Ajax封装

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="http://www.mamicode.com/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    = http://www.mamicode.com/opt.data      || null,>*/</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>

 

Ajax封装