weixin_33717117 2014-06-24 16:01 采纳率: 0%
浏览 865

jQuery Ajax .done()行为

I am wondering about the behavior of the code below. Ajax.chan(tid) is the ajax call found below. The other three functions are not Ajax related. When I don't pass a parameter to Display.channelLoad, the code works as expected and these functions run after the ajax is complete. But if I change that to .done(Display.channelLoad()), this function runs before the ajax call is complete. This is probably some basic javascript knowledge that I am missing, but I would like to pass a parameter to Display.channelLoad and understand what is occurring here.

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'
        });
    }

Thank you.

  • 写回答

1条回答 默认 最新

  • weixin_33675507 2014-06-24 16:11
    关注

    From the jQuery docs on .done:

    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.

    What this means is that in your example the .done(List.articles) gets called with the result of $.ajax(). So it will get called with jqXHR object so it would look like this:

    List.articles(data, textStatus, jqXHR)
    

    And the same will happen for the other done calls in order.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?