javascript - Jquery Ajax .done() 行为

标签 javascript jquery ajax

我想知道下面代码的行为。 Ajax.chan(tid) 是下面的 ajax 调用。其他三个函数与 Ajax 无关。当我不向 Display.channelLoad 传递参数时,代码将按预期工作,并且这些函数将在 ajax 完成后运行。但如果我将其更改为 .done(Display.channelLoad()),则该函数会在 ajax 调用完成之前运行。这可能是我缺少的一些基本 javascript 知识,但我想将参数传递给 Display.channelLoad 并了解这里发生的情况。

AjaxCall.chan(tid).done(List.articles)
                  .done(Display.channelLoad)
                  .done(Display.unblockUI);

AjaxCall.chan()

var AjaxCall = {
    chan: function(tid) {
        return getArticles = $.ajax({
            url: "http://ainonline.com/api/channel/" + tid,
            context: document.body,
            dataType: 'jsonp'
        });
    }

谢谢。

最佳答案

来自 .done 上的 jQuery 文档:

The deferred.done() method accepts one or more arguments, all of which can be either a single function or an array of functions. When the Deferred is resolved, the doneCallbacks are called. Callbacks are executed in the order they were added. Since deferred.done() returns the deferred object, other methods of the deferred object can be chained to this one, including additional .done() methods. When the Deferred is resolved, doneCallbacks are executed using the arguments provided to the resolve or resolveWith method call in the order they were added. For more information, see the documentation for Deferred object.

这意味着在您的示例中,将使用 $.ajax() 的结果调用 .done(List.articles) 。所以它将被调用 jqXHR对象,所以它看起来像这样:

List.articles(data, textStatus, jqXHR)

对于其他按顺序完成的调用也会发生同样的情况。

关于javascript - Jquery Ajax .done() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391306/

相关文章:

javascript - getByTitle() 不返回 Sharepoint 列表

javascript - 文件长度未定义

javascript - 使用选定元素实现滚动效果

php - 可编辑的动态php表

ajax - Coldfusion 在组件中的函数内加密和解密

javascript - 将对象传递给 jQuery.on()

jquery - 如何使用 css 类找到特定元素但使用 Jquery 没有 ID?

javascript - 使用带条件的 jQuery 计算字符串数量

javascript - 如何使用 OData 按部分值进行过滤?

javascript - 使用 div 的值传递数据(安全)