1

次のリンクで示されているように、jQuery の $.ajax 呼び出しは、必要に応じて呼び出しを中止するために後で使用できる ajax オブジェクトを返します。

jQuery を使用して Ajax リクエストを中止する

私のプロジェクトでは、ajax オブジェクト ( $("").load() ) ではなく、オブジェクト パターンを返す $.load() を使用しています。

$.load から ajax オブジェクトを取得する方法はありますか?

4

1 に答える 1

0

現在の API ではそれができないと思います。しかし、オープンソースのおかげで、load関数のソースを見てみましょう (からjquery-1.6.2.js):

jQuery.fn.extend({
load: function( url, params, callback ) {
    if ( typeof url !== "string" && _load ) {
        return _load.apply( this, arguments );

    // Don't do a request if no elements are being requested
    } else if ( !this.length ) {
        return this;
    }

    var off = url.indexOf( " " );
    if ( off >= 0 ) {
        var selector = url.slice( off, url.length );
        url = url.slice( 0, off );
    }

    // Default to a GET request
    var type = "GET";

    // If the second parameter was provided
    if ( params ) {
        // If it's a function
        if ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback
            callback = params;
            params = undefined;

        // Otherwise, build a param string
        } else if ( typeof params === "object" ) {
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );
            type = "POST";
        }
    }

    var self = this;

    // Request the remote document
    jQuery.ajax({
        url: url,
        type: type,
        dataType: "html",
        data: params,
        // Complete callback (responseText is used internally)
        complete: function( jqXHR, status, responseText ) {
            // Store the response as specified by the jqXHR object
            responseText = jqXHR.responseText;
            // If successful, inject the HTML into all the matched elements
            if ( jqXHR.isResolved() ) {
                // #4825: Get the actual response in case
                // a dataFilter is present in ajaxSettings
                jqXHR.done(function( r ) {
                    responseText = r;
                });
                // See if a selector was specified
                self.html( selector ?
                    // Create a dummy div to hold the results
                    jQuery("<div>")
                        // inject the contents of the document in, removing the scripts
                        // to avoid any 'Permission Denied' errors in IE
                        .append(responseText.replace(rscript, ""))

                        // Locate the specified elements
                        .find(selector) :

                    // If not, just inject the full result
                    responseText );
            }

            if ( callback ) {
                self.each( callback, [ responseText, status, jqXHR ] );
            }
        }
    });

    return this;
}
}

少し危険ですが実行可能な解決策は、そのソースをコピーして変更しajaxreturn this. 将来のバージョンで予告なしに変更される可能性がある内部 jquery 動作に依存する可能性があるため、危険です。

おそらく loadAjax など、別の名前の新しい関数で jQuery.fn を拡張するでしょう。

別のバージョンの jquery を使用している場合は、そこから取得してください。

于 2011-09-05T04:41:02.580 に答える