6

ajaxsend イベントで成功関数を上書きしようとしていますが、ここでは機能しません。コードは次のとおりです。

    $(document).ajaxSend(function(event,xhr,options){
        console.log('ajaxSend');
        var tempSuccess = options.success;
        options.success = function(data, textStatus, jqXHR){
            console.log('start');
            tempSuccess(data, textStatus, jqXHR);
            console.log('end');
        }; xhr.success = options.success;});

AJAX では、コンソールに「ajax」が表示されますが、成功すると、開始と終了のデバッグ メッセージが表示されません。

私は何を間違っていますか?

4

3 に答える 3

7

あなたが達成しようとしていることは、で行うことはできませんajaxSend。問題は、元のオブジェクトとオブジェクトajaxSendのコピーで明らかに機能するため、変更しても効果がないことです。次のコードでこれを簡単にテストできます。xhroptions

$(document).ajaxSend(function(event, xhr, options){
    delete options.success;
    console.log(options.success);   // undefined
});
$.ajax({
    url: "test.html",
    success: function() { console.log("this will be printed nevertheless"); }
});


ajaxSendしたがって、成功コールバックを上書きするために 使用することはできません。代わりに、jQueryのAJAX関数を「ハック」する必要があります。

// closure to prevent global access to this stuff
(function(){
    // creates a new callback function that also executes the original callback
    var SuccessCallback = function(origCallback){
        return function(data, textStatus, jqXHR) {
            console.log("start");
            if (typeof origCallback === "function") {
                origCallback(data, textStatus, jqXHR);
            }
            console.log("end");
        };
    };

    // store the original AJAX function in a variable before overwriting it
    var jqAjax = $.ajax;
    $.ajax = function(settings){
        // override the callback function, then execute the original AJAX function
        settings.success = new SuccessCallback(settings.success);
        jqAjax(settings);
    };
})();

$.ajaxこれで、通常どおり簡単に使用できます。

$.ajax({
    url: "test.html",
    success: function() {
        console.log("will be printed between 'start' and 'end'");
    }
});

私の知る限り、jQueryのAJAX関数($.get()またはなど.load())はすべて内部で使用する$.ajaxため、これはjQueryを介して行われるすべてのAJAXリクエストで機能するはずです(ただし、これはテストしていません...)。



そのようなものは、をハッキングすることで「純粋な」JavaScriptでも機能するはずXMLHttpRequest.prototypeです。ActiveXObject以下は、の代わりにを使用するIEでは機能しないことに注意してくださいXMLHttpRequest

(function(){
    // overwrite the "send" method, but keep the original implementation in a variable
    var origSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function(data){
        // check if onreadystatechange property is set (which is used for callbacks)
        if (typeof this.onreadystatechange === "function") {
            // overwrite callback function
            var origOnreadystatechange = this.onreadystatechange;
            this.onreadystatechange = function(){
                if (this.readyState === 4) {
                    console.log("start");
                }
                origOnreadystatechange();
                if (this.readyState === 4) {
                    console.log("end");
                }
            };
        }
        // execute the original "send" method
        origSend.call(this, data);
    };
})();

使用法(通常のXMLHttpRequestと同じように):

var xhr = new XMLHttpRequest();
xhr.open("POST", "test.html", true);
xhr.onreadystatechange = function(){
    if (xhr.readyState === 4) {
        console.log("will be printed between 'start' and 'end'");
    }
};
xhr.send();
于 2012-08-17T17:50:53.797 に答える
-1

クロージャーを使用して、必要なことを達成できます。

function closure(handler) {
    return function(ev, xhr, options) {
        console.log("start");
        handler(ev, xhr, options);
        console.log("stop");
    }
}

$(document).ajaxSend(closure(function(ev, xhr, options) {
    console.log("hello");
}));
于 2012-08-17T17:01:56.600 に答える