3

私は大量の ajax 呼び出しを使用して何かに取り組んでいますが、そのほとんどは同じパラメーターを持っています。jquery.ajaxSetup()を使用してコードを削減することを検討しています。

ただし、jQuery はこのための API を好みません。それは言います:

Note: The settings specified here will affect all calls to $.ajax or AJAX-based 
derivatives such as $.get(). This can cause undesirable behavior since other callers (for 
example, plugins) may be expecting the normal default settings. For that reason we
strongly recommend against using this API. Instead, set the options explicitly in the call
or define a simple plugin to do so.

これを実装して、プラグインを含むすべての js ファイルに触れないようにする方法はありますか? 現在、ajax で動作する外部 jQuery プラグインは使用していませんが、可能であれば、将来の開発者のためにこれを将来保証したいと考えています。jQueryがこれをAPIから削除する可能性はありますか?

4

1 に答える 1

4

次のようなコードで ajax() 呼び出しをラッパーするのはどうですか:

var sendAjax = function (options) {
    var type = 'GET';
    var dataType = 'json';
    var success = options.onsuccess || function () {};
    var error = options.onerror || function () {};
    var url = options.url;
    var data = options.data;
    $.ajax({
        type: type,
        url: url,
        data: data,
        dataType: dataType ,
        success: success ,
        error: error
    });
};

次に、ajax 呼び出しを sendAjax(); に置き換えます。

于 2013-05-20T12:36:45.623 に答える