類比ajax的 script請求

來源:互聯網
上載者:User

標籤:des   style   blog   http   ar   io   color   sp   for   

/**     * 類比ajax的 script請求     * @param  {[type]} options [description]     * @return {[type]}         [description]     */    function createAjax(options) {        if (typeof url === "object") {            options = url;            url = undefined;        }        options = options || {};        /**         * 參數         * jQuery.ajaxSetup 是預設參數         * @type {[type]}         */        var s = jQuery.ajaxSetup({}, options);        // Deferreds        // 非同步機制        var deferred = jQuery.Deferred();        var completeDeferred = jQuery.Callbacks("once memory");        /**         * 實際返回的ajax對象         * @type {Object}         */        var jqXHR = {}        // 把jqXHR對象轉化promise對象,幷加入complete、success、error方法        deferred.promise(jqXHR).complete = completeDeferred.add;        //別名        jqXHR.success = jqXHR.done;        jqXHR.error = jqXHR.fail;        // 增加回調隊列        // complete: function() {        //     console.log(‘局部事件complete‘)        // },        // error: function() {        //     console.log(‘局部事件error請求失敗時調用此函數‘)        // },        // success: function() {        //     console.log(‘局部事件success‘)        // }        for (i in {            success: 1,            error: 1,            complete: 1        }) {            jqXHR[i](s[i]);        }        function send(_, complete) {            var script = jQuery("<script>").prop({                async: true,                charset: s.scriptCharset,                src: s.url            }).on(                "load error",                callback = function(evt) {                    script.remove();                    callback = null;                    if (evt) {                        complete(evt.type === "error" ? 404 : 200, evt.type);                    }                }            );            document.head.appendChild(script[0]);        }        function done(status, nativeStatusText, responses, headers) {            var isSuccess = status >= 200 && status < 300 || status === 304;            var success = jqXHR.success;            var error = jqXHR.error;            if (isSuccess) {                deferred.resolveWith(document, [success, jqXHR]);            } else {                deferred.rejectWith(document, [jqXHR, error]);            }        }        //發送請求        send({            Accept: "text/javascript, application/javascript, application/ecmascri"        }, done);        return jqXHR;    }    function show(data){        $(‘body‘).append(‘<li>‘+ data +‘</li>‘);    }        //執行一個非同步HTTP(Ajax)的請求。        var ajax = createAjax({            url: ‘http://code.jquery.com/jquery-latest.js‘,            dataType: ‘script‘,            //請求完成後回呼函數 (請求success 和 error之後均調用)            complete: function() {                show(‘局部事件complete‘)            },            error: function() {                show(‘局部事件error請求失敗時調用此函數‘)            },            success: function() {                show(‘局部事件success‘)            }        })        ajax.done(function() {           show(‘deferred done‘)        }).fail(function() {            show(‘deferred fail‘)        }).always(function() {            show(‘deferred lways‘)        })

類比ajax的 script請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.