黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 超輕量級的Ajax庫

超輕量級的Ajax庫

更新時間:2021-08-31 10:03:36 來源:動力節(jié)點(diǎn) 瀏覽1892次

主要用法:ajax(options).method()[ajax({url:"getData.jsp",method:"POST",data:"userId=123"}).getScript(function(msg){/****/});]; options:一個對象,包含如下選項(xiàng)

url://數(shù)據(jù)源地址

method://請求方法[POST、HEAD...]

data://要發(fā)送給服務(wù)器的數(shù)據(jù)

async://是否是異步請求

timeout://請求超時,默認(rèn)為十秒

cache://是否從緩存中取數(shù)據(jù)(如果瀏覽器已緩存)

onSuccess:請求成功并將結(jié)果作為參數(shù)調(diào)用的函數(shù)

onError;請求失敗調(diào)用的函數(shù)

onComplete:請求完成調(diào)用的函數(shù),無論成功與否

showStatus;以當(dāng)前狀態(tài)碼為參數(shù)調(diào)用的函數(shù)

type:為本類型,如:txt、js、xml、html

一般情況下url是必需的;其余的可選 ,主要方法:

getText:function(fn)//取文本,請求成功后以獲取的文本為參數(shù)調(diào)用fn函數(shù)

getXML:function(fn)//取XML文本,請求成功后以獲取的XML文檔根對象為參數(shù)調(diào)用fn函數(shù)

getScript:function(fn)//取JavaScript,請求成功后將獲取的代碼用eval執(zhí)行后的結(jié)果為參數(shù)調(diào)用fn函數(shù)

getHTML:function(fn)//取HTML文本,請求成功后以獲取的HTML文本為參數(shù)調(diào)用fn函數(shù),與getText不同的是文本中的HTML標(biāo)記可以被正常顯示

oppendTo(obj)://將返回結(jié)果添加到指定的DOM對象上,如ajax({url:"get.jsp?data=xyz"}).getHTML().appendTo(document.body)

exe:function(options)//發(fā)送和接收請求結(jié)果的核心函數(shù),options是一個對象,包含:

onSuccess:請求成功并將結(jié)果作為參數(shù)調(diào)用的函數(shù)

onError;請求失敗調(diào)用的函數(shù)

onComplete:請求完成調(diào)用的函數(shù),無論成功與否

showStatus;以當(dāng)前狀態(tài)碼為參數(shù)調(diào)用的函數(shù)

type:為本類型,如:txt、js、xml、html

代碼如下:

(function(wnd,undef){
    var doc=wnd.document,OBJ="object",STR="string";           
    var ajax=function(options){
        return new ajax.prototype.init(options);
    };               
    function AjaxError(msg){
        this.name="Ajax錯誤";
        this.message=msg||"未知錯誤";
    }               
    ajax.prototype={
        init:function(option){
            this[0]=this.create();//創(chuàng)建Ajax對象
            this[1]={
                url:option.url||"",//數(shù)據(jù)源地址
                method:option.method||"GET",//請求方法[POST、HEAD...]
                data:option.data||null,//要發(fā)送給服務(wù)器的數(shù)據(jù)
                async:option.async||true,//是否是異步請求
                type:option.type||"text",//返回?cái)?shù)據(jù)后,將數(shù)據(jù)轉(zhuǎn)換為指定的類型.(text,js,xml,html)
                timeout:option.timeout||10000,//請求超時,默認(rèn)為十秒
                cache:option.cache||false,//是否從緩存中取數(shù)據(jù)(如果瀏覽器已緩存)
                onSuccess:option.onSuccess||function(result){},//請求成功后執(zhí)行的函數(shù)(處理返回結(jié)果)
                onError:option.onError||function(){},//請求出錯調(diào)用的函數(shù)
                onComplete:option.onComplete||function(){},//請求完成后(無論成功與否)都執(zhí)行的函數(shù)
                showStatus:option.showStatus||function(){}//顯示請求狀態(tài)
            }; 
            fix(this[1]);
            return this;
        },                   
        create:function(){//創(chuàng)建Ajax對象
            if(wnd.XMLHttpRequest==undef){
                wnd.XMLHttpRequest=function(){
                    if(wnd.ActiveXObject){
                        try{
                            return new ActiveXObject("Msxml2.XMLHTTP");//IE6
                        }catch(e){
                            return new ActiveXObject("Microsoft.XMLHTTP");//IE5
                        }
                    }
                };
            }
            return new  XMLHttpRequest();
        },
        stop:function(){
            try{
                this[0].abort();
            }catch(e){
                throw new AjaxError(e.message)
                }
            return this;
        },
        getText:function(fn){//fn可選
            return this.exe({"onSuccess":fn,"type":"text"});
        },
        getXML:function(fn){
            return this.exe({"onSuccess":fn,"type":"xml"});
        },
        getScript:function(fn){
            return this.exe({"onSuccess":fn,"type":"js"});
        },
        getHTML:function(fn){
            return this.exe({"onSuccess":fn,"type":"html"});
        },
        exe:function(options){
            if(options.onSuccess)this[1].onSuccess=options.onSuccess;
            if(options.onError)this[1].onError=options.onError;
            if(options.onComplete)this[1].onComplete=options.onComplete;
            if(options.showStatus)this[1].showStatus=options.showStatus;
            if(options.type)this[1].type=options.type;
            try{
                var isTimeout=false,cur=this;
                var timer=setTimeout(function(){
                    isTimeout=true;
                    cur.stop();
                    cur[1].onError(new AjaxError("請求超時"));
                },cur[1].timeout);
                //私有方法
                var open=function(){
                    try{
                        cur[0].open(cur[1].method,cur[1].url,cur[1].async);
                        if(/POST/i.test(cur[1].method)){
                            cur[0].setRequestHeader("Content-Type","application/x-www-form-urlencoded");//表單編碼
                            if(cur[0].overrideMimeType)cur[0].setRequestHeader("Connection","close");
                        }
                    }catch(e){
                        throw new AjaxError(e.message);
                    }
                };
                var send=function(){
                    try{
                        cur[0].send(cur[1].data);
                    }catch(e){
                        throw new AjaxError(e.message);
                    }
                };                       
                open();//發(fā)起連接                       
                this[0].onreadystatechange=function(){
                    cur[1].showStatus(cur[0].readyState);
                    if(cur[0].readyState==4&&!isTimeout){                               
                        try{
                            if(isOK(cur[0])){//成功完成
                                var t=httpData(cur[0],cur[1].type);           
                                if(cur.to&&cur.to.length>0){
                                    for(var i=0;i<cur.to.length;i++){
                                        if(cur.to[i].type&&cur.to[i].type=="html")
                                            cur.to[i].target.innerHTML+=t;
                                        else cur.to[i].target.appendChild(doc.createTextNode(t));
                                    }
                                }
                                cur[1].onSuccess(t);
                            }
                            else{
                                cur[1].onError(new AjaxError("請求未成功完成"));
                            }                                     
                        }catch(et){
                            cur[1].onError(new AjaxError(et.message));
                        }finally{
                            cur[1].onComplete();
                            cur[0]=null;
                            clearTimeout(timer);
                        }   
                    }
                };                           
                send();                       
            }catch(e){
                this[1].onError(new AjaxError(e.message));
            }finally{
                return this;
            }                   
        }, 
        appendTo:function(target){//將返回的結(jié)果加到指定的目標(biāo)[id或DOM對象]
            if(!this.to)this.to=[];
            this.to.push({
                "target":$(target),
                "type":this[1].type
                });
            return this;
        }
    };//end prototype
    ajax.prototype.init.prototype=ajax.prototype;          
    ajax.parseToQueryString=function(obj){//將數(shù)組或?qū)ο笮蛄谢?
        if(typeof obj===STR)return obj;
        var s=[];
        if(obj instanceof Array){//假定為數(shù)組
            for(var i=0;i<obj.length;i++)
                s.push(obj[i].name||i+"="+obj[i]);
        }
        else{
            for(var j in obj) s.push(j+"="+obj[j]);
        }
        return s.join("&");
    } ;             
    ajax.parseToObject=function(str){//將查詢字符串轉(zhuǎn)化成對象
        if(typeof str==OBJ)return str;
        var set={};
        str=str.split("&");
        var item;
        for(var i=0;i<str.length;i++){
            if(str[i].indexOf("=")>0){
                item=str[i].split("=");
                set[item[0]]=item[1];
            }
        }
        return set;
    };              
    var fix=function(p){
        if(p.data){
            p.data=ajax.parseToQueryString(p.data);
        }
        if(p.method.toUpperCase()=="GET"&&p.data){
            p.url=append(p.url,p.data);
        }
        if(!p.cache){
            p.url=append(p.url,"abkjfjk="+(new Date().getTime())+"jrejhjdd");
        }
    };              
    var $=function(id){
        return typeof id===OBJ?id:doc.getElementById(id);
    };         
    function isOK(r){
        try{
            return !r.status&&location.protocol=="file:"
            ||(r.status>=200&&r.status<300)
            ||r.status==304
            ||navigator.userAgent.indexOf("Safari")>=0&&r.status==undef;
        }catch(e){}
        return false;
    }             
    function httpData(r,type){
        var res=type;
        if(!res){
            var ct=r.getResponseHeader("Content-Type");
            if(/xml/i.test(ct)) res="xml";
            else if(/JavaScript/i.test(ct))res="js";
            else res="";
        }
        switch(res){
            case "xml":
                return r.responseXML.documentElement;
            case "js":
                return eval("("+r.responseText+")");
            default:
                return r.responseText;
        }   
    }     
    function append(url,param){
        if(url.indexOf("?")<0){
            return url+"?"+param;
        }
        else{
            if(/\?$/.test(url)){
                return url+param;
            }
            else{
                return url+"&"+param;
            }
        }
    }             
    wnd.ajax=ajax;
})(window);

以上就是動力節(jié)點(diǎn)小編介紹的"超輕量級的Ajax庫",希望對大家有幫助,想了解更多可查看AJAX教程。動力節(jié)點(diǎn)在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 精品人人| 中国黄色一级片 | 欧美videos另类极品 | 日韩欧美国产一区二区三区 | 91视频com| 日韩精品中文字幕一区二区三区 | 女人找男人皮日日视频 | 国产成人99精品免费视频麻豆 | 色黄污在线看黄污免费看黄污 | 九九九精品视频 | 日本不卡视频在线播放 | 亚洲国产婷婷综合在线精品 | 97久久人人做人人爽人人澡 | 伊人网在线免费观看 | va天堂| 日韩中文字幕免费版 | 欧美日韩亚洲v在线观看 | 久久伊人影视 | 超级碰在线 | 亚洲夜 | 日韩欧美亚洲另类 | 成人啪精品视频免费网站 | 午夜黄色小视频 | 欧美乱淫 | 免费看黄a级毛片 | 亚洲中文字幕特级毛片 | 久久久香蕉 | 久久国产免费 | 免费观看亚洲 | 免费一级欧美大片视频在线 | 精品国语对白精品自拍视 | 国产精品亚洲欧美大片在线看 | 制服丝袜在线第一页 | 日韩免费视频 | 欧美最新的精品videoss | 看全色黄大色黄大片女图片第一次 | 欧美在线观看免费一区视频 | 免费观看a级毛片在线播放 免费观看a级完整视频 | 成人永久免费视频 | 午夜性片 | 久久99精品波多结衣一区 |