调用ajax:不运行“done”,“fail”或“always”方法

时间:2013-05-29 16:53:18

标签: jquery ajax

(对于刚开始阅读本文的人来说,这个问题已经解决了 - 见底部)

我试图了解这次对ajax的调用是怎么回事。此代码迭代“绑定器”列表,并从服务器中获取每个绑定器。 (现在服务器上只有一个活页夹。)

这是怎么回事:我可以与网页进行大约60秒的延迟,但不会显示任何活页夹。最后一个活页夹出现了,但它的图像缩略图丢失了,我无法与它交互。

从调用“log”到控制台的输出是:

  

Binder:正确的网址,我必须在此审查

在此之后,

  

getAllBinders:以项目

结束

即使最终显示活页夹,也不会打印任何其他内容。这告诉我没有任何方法“完成”,“失败”或“始终”被激活。我知道正在显示一个活页夹,因此代码不能崩溃。 (右?)

以下是代码:

function getAllBinders() {

  var deferred = $.Deferred()

  _.each(binderPreviews, function(item, index) {

    console.log("Binder: "+ajaxPath+" "+item.url)

    $.ajax({
      xhrFields: {
        withCredentials: true
      },
      type: 'get',
      url: ajaxPath+item.url,
      dataType: 'json',
      contentType: "application/json",
      crossDomain: true
    })

    .done(function( data, message, jqxhr ) {

      console.log("getAllBinders Success!");

      // Store the binder record with an index
      $.extend(data, { order: index })

      // Replace null properties with empty strings
      _.each(data, function(value, prop, obj) {
        if( value == null ) obj[ prop ] = ''
      })

      App.Binder.createRecord( data ).save()

      // Check if it's the last call so we can resolve the activity
      if( index == binderPreviews.length - 1 ) {
        deferred.resolve()
        console.log( ConferenceApp.Binder.records() )
      }

      console.log("getAllBinders Index: "+index+", Total: "+binderPreviews.length);
    })

    .fail(function(xhr, status, msg) {
      console.log("getAllBinders Failure: "+status+", "+msg);
      console.log("getAllBinders Index: "+index+", Total: "+binderPreviews.length);
    })

    .always(function() {
      console.log("getAllBinders: ajax.always");
    });

    console.log("getAllBinders: Finished with item");

  })
}

作为一项实验,我在ajax调用中添加了一个不可能的超时(如下所示)。我控制台的输出是:

  

Binder:正确的网址,我必须在此审查

     

getAllBinders:以项目

结束      

getAllBinders失败:超时,超时

     

getAllBinders指数:0,总计:1

     

getAllBinders:ajax.always

    $.ajax({
      xhrFields: {
        withCredentials: true
      },
      type: 'get',
      url: ajaxPath+item.url,
      dataType: 'json',
      contentType: "application/json",
      crossDomain: true,
      timeout: 1 //Test -- this should be impossible
    })
编辑:没关系,我发现了问题:/ App.Binder.createRecord方法中出现运行时错误,一旦运行就崩溃了“完成”方法(但之前没有,这让我很困惑)

0 个答案:

没有答案
相关问题