如何从 jQuery ajax 成功处理程序取消所有后续成功回调?

发布于 2024-12-05 04:17:06 字数 821 浏览 0 评论 0原文

我正在使用 jQuery 1.5.2,并且向 Ajax 请求注册了多个成功回调。在第一个成功回调中,我需要检查响应是否包含我需要的所有数据,如果没有,则取消所有后续成功回调。

注意:我无权访问服务器发送的标头。它始终发送 200 Success

第二个注意事项:我不能只组合这两个回调。这是我在较大系统中遇到的问题的简化示例,该系统在代码中的不同位置注册回调。

如何取消后续的成功回调?

一些示例代码如下所示(jsfiddle):

var request = $.ajax({
    url: '/echo/json'
});

// First success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        // I want to cancel the second success handler here!

    }
});

// Second success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('second', jqXHR);
});

谢谢!

I am using jQuery 1.5.2 and have multiple success callbacks registered to an Ajax request. In the first success callback I need to check if the response has all of the data that I need, and cancel all subsequent success callbacks if it doesn't.

Note: I don't have access to the headers sent by the server. It always sends a 200 Success.

Second Note: I can't just combine the two callbacks. This is a simplified example of a problem that I have with a larger system that registers callbacks in different locations in the code.

How can I cancel the subsequent success callbacks?

Some sample code looks like this (jsfiddle):

var request = $.ajax({
    url: '/echo/json'
});

// First success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        // I want to cancel the second success handler here!

    }
});

// Second success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('second', jqXHR);
});

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殤城〤 2024-12-12 04:17:06

在回调函数中,您应该做的第一件事是检查状态变量(或“标志”),该变量将通知函数在当前情况下是否可以执行。

不想再执行回调?将标志设置为某个值,如果该标志在执行时具有该特定值,则让回调函数立即返回。

或者您可以决定仅运行 XX 回调而不再运行,或者只是暂停它们。

如果您建立这样的系统,您可以从一个位置控制具有多个异步回调的大型 Web 应用程序的行为,它将为您提供很多灵活性和选项。

为了帮助您完成此任务,您可以使用一个充当调度程序的函数。每次您想要更改回调的行为时,此函数将用作回调函数。您将传递正常的回调函数作为参数进行调用,然后调度程序将检查正确的条件,然后决定是否继续正常的回调。

这样您就不需要检查回调函数内部的条件。它对于具有大量回调的大型应用程序特别有用。

In your callback functions the first thing you should do is check for a state variable (or 'flag'), which will inform the function if it can execute or not, given the current situation.

Don't want the callbacks to execute anymore? Set the flag to some value and let the callback functions return immediately if that flag has that particular value when they get executed.

Or you could decide only to run XX callbacks and no more, or just pause them.

If you set up a system like this, you can control from a single place the behavior of larger web applications with multiple asynchronous callbacks, it will give you a lot of flexibility and options.

To aid you in this, you can have a function acting as a dispatcher. This function will be used as a callback function every time you want to alter the behavior of the callbacks. You will pass the normal callback function to call as a parameter, then the dispatcher will check for the right condition before deciding to proceed with the normal callback or not.

This way you don't need to check for the conditions right inside the callback functions. It's especially helpful for large applications with really many callbacks.

清醇 2024-12-12 04:17:06

只是合并。

// Success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        return;
    }
    else {
        console.log('second', jqXHR);
    }
});

或者试试这个:

var request = $.ajax({
    url: '/echo/json'
});
second = true;
// First success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        second = false;
    }
});

// Second success handler
request.done(function(data, textStatus, jqXHR) {
    if(second)
        console.log('second', jqXHR);
});

小提琴: http://jsfiddle.net/maniator/zGvPW/3/

Just merge.

// Success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        return;
    }
    else {
        console.log('second', jqXHR);
    }
});

Or Try this:

var request = $.ajax({
    url: '/echo/json'
});
second = true;
// First success handler
request.done(function(data, textStatus, jqXHR) {
    console.log('first', jqXHR);
    if ($.isEmptyObject(data)) {
        second = false;
    }
});

// Second success handler
request.done(function(data, textStatus, jqXHR) {
    if(second)
        console.log('second', jqXHR);
});

Fiddle: http://jsfiddle.net/maniator/zGvPW/3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文