My promises no longer working in jQuery 1.8

rya*_*yan 6 javascript ajax jquery jquery-deferred

This code snippet worked in 1.7.2 with both success/error callbacks as well as promises style callbacks. With 1.8.2 the success/error callbacks still work but the promises do not. My hunch is that the return dfd.promise(jqXHR); line is the problem but im not certain.

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {

    // Don't infinitely recurse
    originalOptions._retry = isNaN(originalOptions._retry)
        ? Common.auth.maxExpiredAuthorizationRetries
        : originalOptions._retry - 1;

    // set up to date authorization header with every request
    jqXHR.setRequestHeader("Authorization", Common.auth.getAuthorizationHeader());

    // save the original error callback for later
    if (originalOptions.error)
        originalOptions._error = originalOptions.error;

    // overwrite *current request* error callback
    options.error = $.noop();

    // setup our own deferred object to also support promises that are only invoked
    // once all of the retry attempts have been exhausted
    var dfd = $.Deferred();
    jqXHR.done(dfd.resolve);

    // if the request fails, do something else yet still resolve
    jqXHR.fail(function () {
        var args = Array.prototype.slice.call(arguments);

        if (jqXHR.status === 401 && originalOptions._retry > 0) {

            // refresh the oauth credentials for the next attempt(s)
            // (will be stored and returned by Common.auth.getAuthorizationHeader())
            Common.auth.handleUnauthorized();

            // retry with our modified
            $.ajax(originalOptions).then(dfd.resolve, dfd.reject);

        } else {
            // add our _error callback to our promise object
            if (originalOptions._error)
                dfd.fail(originalOptions._error);
            dfd.rejectWith(jqXHR, args);
        }
    });

    // NOW override the jqXHR's promise functions with our deferred
    return dfd.promise(jqXHR);
});
Run Code Online (Sandbox Code Playgroud)

Update: Here is my ajax request that fails:

$.ajax({
        url: someFunctionToGetUrl(),
        // works
        //success: callback,
        //error: ajaxErrorHandler
    }).then(
        [callback],
        [errorback, ajaxErrorHandler]
    );
};
Run Code Online (Sandbox Code Playgroud)

gil*_*ly3 7

编辑:这是一个文档错误,但行为是设计的.api改变了,deferred.then现在表现得像deferred.pipe并且不再允许传入数组,但文档尚未更新以反映这一点.

相关错误:

我在下面的原始答案结尾处描述的解决方法仍然适用.


原始答案:

它对我来说看起来像一个jQuery错误.如果传入单个函数引用作为第一个参数,它可以工作,但如果传入一个函数数组则不行:

http://jsfiddle.net/tunDH/

但是,文档说一系列函数就好了:

doneCallbacks   解析Deferred时调用的函数或函数数组.

而且,你是对的.它适用于jQuery 1.7:http://jsfiddle.net/tunDH/1/

解决方法是将所有函数调用包装在单个函数中,而不是在数组中:

$.ajax({ 
    url: someFunctionToGetUrl(), 
    // works 
    //success: callback, 
    //error: ajaxErrorHandler 
}).then( 
    function(){
        callback1.apply(this, arguments);
        callback2.apply(this, arguments);
    }, 
    [errorback, ajaxErrorHandler] 
); 
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/tunDH/2/

您可能需要对错误回调执行相同的操作,但我没有测试它.