【问题标题】:jQuery ajax done callback behaves strange in firefoxjQuery ajax done 回调在 Firefox 中的行为很奇怪
【发布时间】:2013-10-11 12:36:32
【问题描述】:

我对 jQuery 1.7.1 和 firefox 中的 ajax 函数有疑问。 我正在做一个从 CMIS 服务器请求 json 的 ajax 调用。在 chrome 中一切正常。举个例子吧:

function ajaxCall(url, requestType, isAsync, parameters, doneCb, failCb) {
    $.ajax(url, {
        type: requestType,
        async: isAsync,
        data: parameters
    }).done(
        alert("test"),
        // the following function gets not executed in firefox with async: true
        function (result) {
            doneCb(result);
        }).fail(function (cause) {
            failCb(cause);
        });
}

firefox 的奇怪之处在于,如果我使用 async: false ,一切都会像魅力一样工作。如果我使用 async: true ,也会执行 done 回调,并弹出带有消息“test”的警报。但是警报后的功能没有被执行。 我google了很多,找不到解决这个问题的方法。

你是我最后的希望;)

感谢和最好的问候, 西蒙

我也尝试过老式的方法,但结果还是一样 - 如果 async 为 true,则不会在 firefox 中调用成功函数。

 $.ajax(url, {
        type: requestType,
        async: isAsync,            
        data: parameters,
        success: function(result){
            doneCb(result);
        },
        error: function(cause){
            failCb(cause);
        }
    });

【问题讨论】:

  • 删除了我的答案。刚刚注意到您使用的是 Jquery 1.7。此建议适用于 Jquery 1.8+。对不起

标签: javascript jquery ajax asynchronous


【解决方案1】:

该代码采用alert 并将其返回的内容分配给done 的第一个参数。它在函数初始化时执行,而不是在实际调用 done() 方法时执行。

应该是这样的

.done( 
    function() { alert("test") },
    function() { ... }
)

【讨论】:

  • 感谢您的回复。在我的原始代码中,我没有警报。我只是把它放在那里看看发生了什么。如果我在没有警报的情况下执行代码,则也永远不会调用该函数:(
猜你喜欢
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2011-08-18
  • 2014-08-31
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多