0

これが私のコードです:

new Ajax.Updater('container', url, {
    method: "get",
    onLoading: function () {
        document.getElementById('container').innerHTML = "Loading...";
    },
    on200: function(response) {
        if(response.responseText.match(/WhatImLookingFor/)) {
            window.location = "leNewURL";
        }
    },
    onComplete: function(response) {
        //do other cool stuff
    }
});

私がやろうとしているのは、container更新される前に応答をインターセプトすることです。応答のテキストが一致する場合はWhatImLookingFor、ユーザーをにリダイレクトしますleNewURL。上記のコードでは、これは発生していますが、更新前ではないため、風変わりに見えます。とにかく、更新が行われる直前に傍受できるものはありますか、containerそれとも、一致するものがない場合にのみ非表示にして表示するなど、他のハックに頼る必要がありますか?

4

1 に答える 1

1

そのようにAjax呼び出しの動作をカスタマイズしたい場合は、ベースAjax.Request() http://api.prototypejs.org/ajax/Ajax/Request/を使用することをお勧めします。

new Ajax.Request(url, {
    method: "get",
    onLoading: function () {
        $('container').update("Loading...");
    },
    onComplete: function(response) {
        if(response.responseText.match(/WhatImLookingFor/)) {
            window.location = "leNewURL";
        }
        else {
            //do other cool stuff
            $('container').update(response.responseText);
        }
    }
});

タイプするのが少ないユーティリティメソッドと交換し、PrototypeJSのすべてのElementメソッドが含まれてdocument.getElementById()います$()

于 2013-03-26T22:28:27.260 に答える