【问题标题】:obtaining jQuery $.load() ajax object获取 jQuery $.load() ajax 对象
【发布时间】:2011-09-05 04:29:24
【问题描述】:

如以下链接所示,jQuery 的 $.ajax 调用返回一个 ajax 对象,以后可以在需要时中止调用。

Abort Ajax requests using jQuery

我的项目正在使用 $.load(),它返回对象模式,而不是 ajax 对象 ($("").load())。

有没有办法从 $.load 获取 ajax 对象?

【问题讨论】:

  • 为什么不能改为使用 $.ajax 来使用相同的方法?
  • 我有一个在任何地方都使用 $.load() 的项目,将 $.load() 更改为 $.ajax() 并不是一个好主意。如果 $.load() 能以某种方式返回 jqXHR 对象来取消请求,那就太好了。

标签: jquery ajax abort


【解决方案1】:

我认为您无法使用当前的 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;
}
}

一个稍微危险但可行的解决方案是复制该源并对其进行更改,以便它返回ajax 调用返回的内容,而不是return this。这是危险的,因为它可能依赖于内部 jquery 行为,在未来的版本中可能会更改,恕不另行通知。

我可能会用一个不同名称的新函数来扩展 jQuery.fn,也许是 loadAjax

如果您使用的是不同版本的 jquery,请从那里获取。

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2012-02-11
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2011-01-12
    相关资源
    最近更新 更多