您好,登錄后才能下訂單哦!
小編給大家分享一下jquery中Ajax全局調(diào)用封裝的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
前言:
有一種情況:全站都要用異步方式來(lái)調(diào)用 數(shù)據(jù),提交數(shù)據(jù),那么你每次操作 都會(huì)要$.ajax({.....})
寫(xiě)重復(fù)的方法 和代碼,冗余太大, 也浪費(fèi)時(shí)間,雖說(shuō)你有代碼自動(dòng)提示補(bǔ)全,但真的不優(yōu)雅,身為前端極客,是不能允許的!
jQuery Ajax通用js封裝
第一步:引入jQuery庫(kù)
<script type="text/javascript" src="/js/jquery.min.js"></script>
第二步:開(kāi)發(fā)Ajax封裝類(lèi),已測(cè)試通過(guò),可以直接調(diào)用,直接貼代碼,講解就省了
/***************************************************************** jQuery Ajax封裝通用類(lèi) (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ù)類(lèi)型,常用的如: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ù)類(lèi)型,常用的如: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封裝通用類(lèi)測(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ù)工作和代碼冗余。
以上是“jquery中Ajax全局調(diào)用封裝的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。