本文大概分為三步實現jquery ajax通過js封裝,通過代碼執行個體講解,代碼附有注釋,比較容易理解,具體詳情如下所示:
第一步:引入jQuery庫
<script type="text/javascript" src="<%=path%>/resources/js/jquery.min.js"></script>
第二步:開發Ajax封裝類,已測試通過,可以直接調用,直接貼代碼,講解就省了
/*****************************************************************jQuery Ajax封裝通用類 (linjq) *****************************************************************/$(function(){/*** ajax封裝* url 發送請求的地址* data 發送到伺服器的資料,數組儲存,如:{"date": new Date().getTime(), "state": 1}* async 預設值: true。預設設定下,所有請求均為非同步請求。如果需要發送同步請求,請將此選項設定為 false。* 注意,同步請求將鎖住瀏覽器,使用者其它操作必須等待請求完成才可以執行。* type 請求方式("POST" 或 "GET"), 預設為 "GET"* dataType 預期伺服器返回的資料類型,常用的如:xml、html、json、text* successfn 成功回呼函數* errorfn 失敗回呼函數*/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 發送請求的地址* data 發送到伺服器的資料,數組儲存,如:{"date": new Date().getTime(), "state": 1}* successfn 成功回呼函數*/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 發送請求的地址* data 發送到伺服器的資料,數組儲存,如:{"date": new Date().getTime(), "state": 1}* dataType 預期伺服器返回的資料類型,常用的如:xml、html、json、text* successfn 成功回呼函數* errorfn 失敗回呼函數*/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);}});};});
第三步:調用類比
<%@ 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封裝通用類測試</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("出錯了");});$.axs(getRootPath()+"/test/ajax.html", null, function(data){alert(data.data);});$.axse(getRootPath()+"/test/ajax.html",null, function(){alert("成功了");},function(){alert("出錯了");});});</script></head><body></body></html>
以上所述是小編給大家介紹的jQuery Ajax通用js封裝的相關知識,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!