淺析jQuery Ajax通用js封裝
本文大概分為三步實(shí)現(xiàn)jquery ajax通過(guò)js封裝,通過(guò)代碼實(shí)例講解,代碼附有注釋,比較容易理解,具體詳情如下所示:
第一步:引入jQuery庫(kù)
<script type="text/javascript" src="<%=path%>/resources/js/jquery.min.js"></script>
第二步:開發(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.axs=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.axse=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)用模擬
<%@ page language="java" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <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ò)了"); } ); $.axs(getRootPath()+"/test/ajax.html", null, function(data){ alert(data.data); }); $.axse(getRootPath()+"/test/ajax.html", null, function(){ alert("成功了"); }, function(){ alert("出錯(cuò)了"); }); }); </script> </head> <body> </body> </html>
以上所述是小編給大家介紹的jQuery Ajax通用js封裝的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 一個(gè)AJAX自動(dòng)完成功能的js封裝源碼[支持中文]
- 原生Javascript封裝的一個(gè)AJAX函數(shù)分享
- js實(shí)現(xiàn)對(duì)ajax請(qǐng)求面向?qū)ο蟮姆庋b
- 原生JS封裝ajax 傳json,str,excel文件上傳提交表單(推薦)
- JavaScript 封裝Ajax傳遞的數(shù)據(jù)代碼
- js鎖屏解屏通過(guò)對(duì)$.ajax進(jìn)行封裝實(shí)現(xiàn)
- js原生Ajax的封裝和原理詳解
- 使用原生js封裝的ajax實(shí)例(兼容jsonp)
- 純js封裝的ajax功能函數(shù)與用法示例
- 原生javascript實(shí)現(xiàn)的ajax異步封裝功能示例
- 原生js封裝的ajax方法示例
相關(guān)文章
jquery實(shí)現(xiàn)動(dòng)態(tài)菜單的實(shí)例代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)動(dòng)態(tài)菜單的實(shí)例代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11Jquery下的26個(gè)實(shí)用小技巧(jQuery tips, tricks & solutions)
前段時(shí)間發(fā)布了Jquery類庫(kù)1.4版本,使用者也越來(lái)越多,為了方便大家對(duì)Jquery的使用,下面列出了一些Jquery使用技巧。2010-03-03jquery實(shí)現(xiàn)簡(jiǎn)單實(shí)用的彈出層效果代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)簡(jiǎn)單實(shí)用的彈出層效果代碼,涉及jquery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10jQuery實(shí)現(xiàn)刪除li節(jié)點(diǎn)的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)刪除li節(jié)點(diǎn)的方法的相關(guān)資料,需要的朋友可以參考下2016-12-12jQuery實(shí)現(xiàn)獲取動(dòng)態(tài)添加的標(biāo)簽對(duì)象示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)獲取動(dòng)態(tài)添加的標(biāo)簽對(duì)象,涉及jQuery針對(duì)頁(yè)面元素的動(dòng)態(tài)添加、元素獲取與事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2018-06-06javascript 全角轉(zhuǎn)換實(shí)現(xiàn)代碼
當(dāng)客戶端用戶切換輸入法至全角時(shí)可能您的表單提交會(huì)有漏洞哦!不過(guò)事實(shí)上js有這功能 可以將其轉(zhuǎn)換為非全角字符!2009-07-07jQuery使用before()和after()在元素前后添加內(nèi)容的方法
這篇文章主要介紹了jQuery使用before()和after()在元素前后添加內(nèi)容的方法,實(shí)例分析了jQuery中before()和after()方法添加內(nèi)容的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03