jQuery Ajax 全局調(diào)用封裝實(shí)例代碼詳解
有一種情況:全站都要用異步方式來(lái)調(diào)用 數(shù)據(jù),提交數(shù)據(jù),那么你每次操作 都會(huì)要$.ajax({.....})
寫(xiě)重復(fù)的方法 和代碼,冗余太大, 也浪費(fèi)時(shí)間,雖說(shuō)你有代碼自動(dòng)提示補(bǔ)全,但真的不優(yōu)雅,身為前端極客,是不能允許的!
【嘿嘿!雖說(shuō)我現(xiàn)在基本不用jquery了 ,不過(guò)異步概念 是永遠(yuǎn)要用的,就幫助下新人】
jQuery Ajax通用js封裝
第一步:引入jQuery庫(kù)
<script type="text/javascript" src="/js/jquery.min.js"></script>
第二步:開(kāi)發(fā)Ajax封裝類,已測(cè)試通過(guò),可以直接調(diào)用,直接貼代碼,講解就省了
/***************************************************************** jQuery Ajax封裝通用類 (linjq) *****************************************************************/ $(function(){ /** * ajax封裝 * url 發(fā)送請(qǐng)求的地址 * data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲(chǔ),如:{"date": new Date().getTime(), "state": 1} * async 默認(rèn)值: true。默認(rèn)設(shè)置下,所有請(qǐng)求均為異步請(qǐng)求。如果需要發(fā)送同步請(qǐng)求,請(qǐng)將此選項(xiàng)設(shè)置為 false。 * 注意,同步請(qǐng)求將鎖住瀏覽器,用戶其它操作必須等待請(qǐng)求完成才可以執(zhí)行。 * type 請(qǐng)求方式("POST" 或 "GET"), 默認(rèn)為 "GET" * dataType 預(yù)期服務(wù)器返回的數(shù)據(jù)類型,常用的如:xml、html、json、text * successfn 成功回調(diào)函數(shù) * errorfn 失敗回調(diào)函數(shù) */ jQuery.ax=function(url, data, async, type, dataType, successfn, errorfn) { async = (async==null || async=="" || typeof(async)=="undefined")? "true" : async; type = (type==null || type=="" || typeof(type)=="undefined")? "post" : type; dataType = (dataType==null || dataType=="" || typeof(dataType)=="undefined")? "json" : dataType; data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: type, async: async, data: data, url: url, dataType: dataType, success: function(d){ successfn(d); }, error: function(e){ errorfn(e); } }); }; /** * ajax封裝 * url 發(fā)送請(qǐng)求的地址 * data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲(chǔ),如:{"date": new Date().getTime(), "state": 1} * successfn 成功回調(diào)函數(shù) */ jQuery.axpost=function(url, data, successfn) { data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: "post", data: data, url: url, dataType: "json", success: function(d){ successfn(d); } }); }; /** * ajax封裝 * url 發(fā)送請(qǐng)求的地址 * data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲(chǔ),如:{"date": new Date().getTime(), "state": 1} * dataType 預(yù)期服務(wù)器返回的數(shù)據(jù)類型,常用的如:xml、html、json、text * successfn 成功回調(diào)函數(shù) * errorfn 失敗回調(diào)函數(shù) */ jQuery.axspost=function(url, data, successfn, errorfn) { data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: "post", data: data, url: url, dataType: "json", success: function(d){ successfn(d); }, error: function(e){ errorfn(e); } }); }; });
第三步:調(diào)用模擬
<!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <title>jQuery Ajax封裝通用類測(cè)試</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <jsp:include page="/view/common/js_taglib.jsp"></jsp:include> <script type="text/javascript"> $(function(){ $.ax( getRootPath()+"/test/ajax.html", null, null, null, null, function(data){ alert(data.code); }, function(){ alert("出錯(cuò)了"); } ); $.axpost(getRootPath()+"/test/ajax.html", null, function(data){ alert(data.data); }); $.axspost(getRootPath()+"/test/ajax.html", null, function(){ alert("成功了"); }, function(){ alert("出錯(cuò)了"); }); }); </script> </head> <body> </body> </html> $.axpost(getRootPath()+"/test/ajax.html", null, function(data){ alert(data.data); });
如上代碼:只要填寫(xiě) url,和要傳輸?shù)?data 字段就行了,避免了重復(fù)工作和代碼冗余。
以上內(nèi)容是小編給大家介紹的jQuery Ajax 全局調(diào)用封裝實(shí)例代碼詳解的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!
相關(guān)文章
Jquery鼠標(biāo)放上去顯示全名的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Jquery鼠標(biāo)放上去顯示全名的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02JQuery實(shí)現(xiàn)的圖文自動(dòng)輪播效果插件
這篇文章主要介紹了JQuery實(shí)現(xiàn)的圖文自動(dòng)輪播效果插件,涉及jQuery處理鼠標(biāo)事件及動(dòng)畫(huà)效果的方法,需要的朋友可以參考下2015-06-06jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法
這篇文章主要介紹了jQuery+php實(shí)時(shí)獲取及響應(yīng)文本框輸入內(nèi)容的方法,涉及jQuery響應(yīng)鍵盤(pán)事件及ajax調(diào)用php文件針對(duì)輸入內(nèi)容的處理與回調(diào)相關(guān)技巧,非常簡(jiǎn)單易懂,需要的朋友可以參考下2016-05-05JQuery擴(kuò)展插件Validate 2通過(guò)參數(shù)設(shè)置驗(yàn)證規(guī)則
在前面示例中使用的的方法簡(jiǎn)單方便,但沒(méi)有完全將js與頁(yè)面結(jié)構(gòu)完全分離,也就是說(shuō)js依賴了class,下面通過(guò)validate()方法的參數(shù)設(shè)置驗(yàn)證規(guī)則將js與頁(yè)面結(jié)構(gòu)完全分離2011-09-09jQuery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)浮層功能示例【拖動(dòng)div等任何標(biāo)簽】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)浮層功能,可實(shí)現(xiàn)拖動(dòng)div等任何標(biāo)簽的效果,涉及jQuery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-12-12jquery使用正則表達(dá)式驗(yàn)證email地址的方法
這篇文章主要介紹了jquery使用正則表達(dá)式驗(yàn)證email地址的方法,以實(shí)例形式分析了正則表達(dá)式的使用技巧,需要的朋友可以參考下2015-01-01