jQuery Ajax 全局調(diào)用封裝實例代碼詳解
來源:易賢網(wǎng) 閱讀:1562 次 日期:2016-06-16 16:35:51
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery Ajax 全局調(diào)用封裝實例代碼詳解”,方便廣大網(wǎng)友查閱!

有一種情況:全站都要用異步方式來調(diào)用 數(shù)據(jù),提交數(shù)據(jù),那么你每次操作 都會要$.ajax({.....})

寫重復(fù)的方法 和代碼,冗余太大, 也浪費時間,雖說你有代碼自動提示補全,但真的不優(yōu)雅,身為前端極客,是不能允許的!

【嘿嘿!雖說我現(xiàn)在基本不用jquery了 ,不過異步概念 是永遠要用的,就幫助下新人】

jQuery Ajax通用js封裝

第一步:引入jQuery庫

<script type="text/javascript" src="/js/jquery.min.js"></script>

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

/*****************************************************************

jQuery Ajax封裝通用類 (linjq) 

*****************************************************************/

$(function(){

/**

* ajax封裝

* url 發(fā)送請求的地址

* data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲,如:{"date": new Date().getTime(), "state": 1}

* async 默認值: true。默認設(shè)置下,所有請求均為異步請求。如果需要發(fā)送同步請求,請將此選項設(shè)置為 false。

* 注意,同步請求將鎖住瀏覽器,用戶其它操作必須等待請求完成才可以執(zhí)行。

* type 請求方式("POST" 或 "GET"), 默認為 "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ā)送請求的地址

* data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲,如:{"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ā)送請求的地址

* data 發(fā)送到服務(wù)器的數(shù)據(jù),數(shù)組存儲,如:{"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封裝通用類測試</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("出錯了");

}

);

$.axpost(getRootPath()+"/test/ajax.html", null, function(data){

alert(data.data);

});

$.axspost(getRootPath()+"/test/ajax.html",

null, 

function(){

alert("成功了");

},

function(){

alert("出錯了");

});

});

</script>

</head>

<body>

</body>

</html>

$.axpost(getRootPath()+"/test/ajax.html", null, function(data){

alert(data.data);

});

如上代碼:只要填寫 url,和要傳輸?shù)?data 字段就行了,避免了重復(fù)工作和代碼冗余。

以上內(nèi)容是小編給大家介紹的jQuery Ajax 全局調(diào)用封裝實例代碼詳解的相關(guān)內(nèi)容,希望對大家有所幫助

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:jQuery Ajax 全局調(diào)用封裝實例代碼詳解

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)