jQuery版AJAX簡易封裝

來源:互聯網
上載者:User

標籤:

開發過程中,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);            }        });    };});

 

jQuery版AJAX簡易封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.