成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? 淺析jQuery Ajax通用js封裝
全站頭部文字 我要出現(xiàn)在這里
淺析jQuery Ajax通用js封裝 661 2024-02-27   

本文大概分為三步實現(xiàn)jquery ajax通過js封裝,通過代碼實例講解,代碼附有注釋,比較容易理解,具體詳情如下所示:

第一步:引入jQuery庫

<script type="text/javascript" src="http://www.gimoo.net/t/1902/<%=path%>/resources/js/jquery.min.js"></script> 

第二步:開發(fā)Ajax封裝類,已測試通過,可以直接調用,直接貼代碼,講解就省了

/*****************************************************************
jQuery Ajax封裝通用類 (linjq) 
*****************************************************************/
$(function(){
/**
* ajax封裝
* url 發(fā)送請求的地址
* data 發(fā)送到服務器的數(shù)據(jù),數(shù)組存儲,如:{"date": new Date().getTime(), "state": 1}
* async 默認值: true。默認設置下,所有請求均為異步請求。如果需要發(fā)送同步請求,請將此選項設置為 false。
* 注意,同步請求將鎖住瀏覽器,用戶其它操作必須等待請求完成才可以執(zhí)行。
* type 請求方式("POST" 或 "GET"), 默認為 "GET"
* dataType 預期服務器返回的數(shù)據(jù)類型,常用的如:xml、html、json、text
* successfn 成功回調函數(shù)
* errorfn 失敗回調函數(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ā)送請求的地址
* data 發(fā)送到服務器的數(shù)據(jù),數(shù)組存儲,如:{"date": new Date().getTime(), "state": 1}
* successfn 成功回調函數(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ā)送請求的地址
* data 發(fā)送到服務器的數(shù)據(jù),數(shù)組存儲,如:{"date": new Date().getTime(), "state": 1}
* dataType 預期服務器返回的數(shù)據(jù)類型,常用的如:xml、html、json、text
* successfn 成功回調函數(shù)
* errorfn 失敗回調函數(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);
}
});
};
}); 

第三步:調用模擬

<%@ 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 >
<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封裝的相關知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對綠夏網(wǎng)網(wǎng)站的支持!



主站蜘蛛池模板: 男吸女人奶水视频免费观看| www.黄视频| 免费观看淫www视频| 远景山谷1981免费版| 抖音社区| 小淘气尼古拉| 膨腹爱好者撑肚子视频| 赵依芳| 黄子华最新电影| 电影继母劳拉| 邻家花美男 电视剧| 变形金刚6免费完整版在线观看| 普及的意思三年级下册语文| 爆操处女| 久草在现| 浙江卫视今天全部节目表| 二年级最佳家长评语| 欧美日韩欧美日韩在线观看视频| 富贵不能淫翻译| 女同版痴汉电车| 山楂树之恋电影剧情简介| 蜘蛛侠4英雄无归| 百字明咒注音全文读诵| 整容前后| 极寒之城剧情详细介绍| 谭凯琪| 抖音登录网页版| 拉字至上q世代| 风俗通| 《卜算子》全文加拼音| 贾林| 六级词汇电子版| 今年过年时间| 刀客家族| 一直很安静简谱| 低糖食物一览表| 王熙| 追龙演员表全部名单| 贝瓦儿歌第一季| 保镖 电影在线观看 完整版| 女生摸女生的胸|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103