国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery版AJAX簡易封裝代碼

2019-11-20 09:00:22
字體:
來源:轉載
供稿:網友

開發過程中,AJAX的應用應該說非常頻繁,當然,jQuery的AJAX函數已經非常好用,但是小編還是稍微整理下,方便不同需求下,可以簡化輸入參數,下面是實例代碼:

$(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.syncAjax=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.jsonAjax=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.jsonAjax2=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);      }    });  };});

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南华县| 溧水县| 枞阳县| 衢州市| 宣化县| 双峰县| 霍山县| 平昌县| 昌都县| 正蓝旗| 临武县| 昭觉县| 荃湾区| 贵南县| 方城县| 清丰县| 万载县| 永丰县| 公主岭市| 烟台市| 定西市| 佛山市| 涿州市| 安龙县| 丰宁| 鱼台县| 镇宁| 崇明县| 磐安县| 泗洪县| 余姚市| 临清市| 甘孜县| 江城| 镇巴县| 五华县| 旌德县| 澄江县| 正定县| 东辽县| 长沙市|