码迷,mamicode.com
首页 > Web开发 > 详细

Ajax封装

时间:2015-01-24 15:43:00      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

 

Ajax封装

标签:

原文地址:http://www.cnblogs.com/anson0415/p/4245932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!