简体   繁体   English

AJAX函数呼吁成功

[英]AJAX function calling on success

My javascript code - 我的javascript代码 -

function updateWhatIfPrivacyLevelRemove(recordId, correspondingDetailIDs) {
    var ajaxCall = $.ajax({ data: { Svc: cntnrRoot,
        Cmd: 'updateWhatIfPrivacyLevel',
        updatePrivacyAction: 'Remove',
        recordID: recordID
        },
        dataType: "text",
        context: this,
        cache: false
    });

    $.when(ajaxCall).then(updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs));
}

function updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs) {
    //several other lines of non-related code
            $.ajax({ data: { Svc: cntnrRoot,
                Cmd: 'handleResidentRow',
                recordID: 1,
                active: 0
            },
                dataType: "text",
                context: this,
                cache: false
            });
}

within my C# code I handle the call backs for 'updateWhatIfPrivacyLevel' and 'handleResidentRow'. 在我的C#代码中,我处理'updateWhatIfPrivacyLevel'和'handleResidentRow'的回调。 I can tell that the AJAX callback to handleResidnetRow is called before updateWhatIfPrivacyLevel. 我可以告诉在updateWhatIfPrivacyLevel之前调用handleResidnetRow的AJAX回调。

Why? 为什么?

When you're trying to set up the callback, you're actually calling the function. 当您尝试设置回调时,您实际上正在调用该函数。 In other words, you're not passing that "updateWhatIf..." function as the callback, you're passing in its return value (which looks like it'd always be undefined ). 换句话说,你没有将“updateWhatIf ...”函数作为回调传递,你传递的是它的返回值(看起来总是undefined )。

Try this instead: 试试这个:

$.when(ajaxCall).then(function() {
  updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs);
});

A reference to a function name is a reference to the function as object, and can be used to pass the function as a callback. 对函数名的引用是对函数的引用,可用于将函数作为回调传递。 However, a reference to a function followed by ( ) is a call to the function, which will be evaluated so that the return value can be used in the context of the surrounding expression. 但是,对函数后跟( )的引用是对函数的调用 ,将对其进行求值,以便可以在周围表达式的上下文中使用返回值。 Thus, in your code, you pass undefined (the result of the function call) to the .then() method, which of course won't do what you want. 因此,在你的代码中,你将undefined (函数调用的结果)传递给.then()方法,当然这不会做你想要的。

It's important to keep in mind that jQuery is just JavaScript, and in particular a JavaScript function library. 重要的是要记住,jQuery只是JavaScript,特别是JavaScript函数库。 Though the .then() thing looks like a language construct, it isn't — the JavaScript interpreter doesn't treat it specially in any way. 虽然.then() 看起来像一个语言结构,但它不是 - JavaScript解释器不会以任何方式特别对待它。

An alternative to using an anonymous function as in my suggestion is to use the .bind() method on the Function prototype in newer browsers. 在我的建议中使用匿名函数的替代方法是在较新的浏览器中对Function原型使用.bind()方法。 That basically does the same thing for you, but it's stylistically more like traditional functional programming. 这基本上对你来说是一样的,但它在风格上更像传统的函数式编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM