【问题标题】:jQuery $.when always fails : what is $.when looking for to determine success/failure?jQuery $.when 总是失败:什么是 $.when 寻找来确定成功/失败?
【发布时间】:2011-11-09 17:04:04
【问题描述】:

我需要链接 2 个帖子的结果,并从以下位置的延迟样本开始:http://api.jquery.com/jQuery.when/

var successFunction = function (event) { alert(event.readyState);  };
var failedFunction = function (event) { alert(event.readyState); };
$.when($.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
  .then(successFunction , failedFunction );

在我的情况下,即使事件对象和 chrome 报告以下属性,failedFunction 也会始终触发:

readyState: 4
responseText: "OK"
status: 200
statusText: "OK"

使用以下形式时结果相同:

$.when( $.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
   .then(successFunction)
   .fail(failFunction);

什么是 $.when 寻找来确定成功/失败?当 readyState === 4 和 status === 200 时,我如何获得 $.when 来触发 successFunction?它是否在寻找我没有从服务器发送的其他内容?

【问题讨论】:

  • 也许第二个 AJAX 调用失败了。试试var failedFunction = function(a, b) { alert(b.status);};

标签: ajax jquery http-status-codes jquery-deferred


【解决方案1】:

jQuery 在检查 ajax 请求是否成功时会查找以下状态码

status >= 200 && status < 300 || status === 304   

除此之外的任何事情都将被视为不成功。

由于您有两个 ajax 请求,我怀疑其中一个请求失败。

尝试记录响应并查看哪个特定的响应失败。

$.when($.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST')).done(function(a1,  a2){
    /* arguments are [ "success", statusText, jqXHR ] */
   console.log( a1[2] ); 
   console.log( a2[2] ); 
});

【讨论】:

    【解决方案2】:

    感谢您的帮助。我在每个 .ajax 调用中添加了一个错误:function(xhr, ajaxOptions, throwedError),这给了我更多详细信息。两者都返回相同的错误“Unexpected token O”,因为我返回的是“OK”。一旦我删除了退货,问题就消失了。没有机会尝试完成的技术,但我相信这对将来会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2013-11-29
      相关资源
      最近更新 更多