如何在第一次成功和更新页面上调用第二个jQuery.ajax实例

时间:2022-12-01 10:59:54

I have some jQuery that is triggered on click of a link with the class 'changetag'. I'm using $.ajax() to update the database via changetag.php.

我点击了一个带有'changetag'类的链接就触发了一些jQuery。我正在使用$ .ajax()通过changetag.php更新数据库。

I then change the visual appearance of the link by toggling the class between on/off. The code is as follows:

然后我通过在开/关之间切换类来改变链接的视觉外观。代码如下:

$(function() {
$(".changetag").click(function(){
    var element = $(this);
    var I = element.attr("id");
    var info = 'switch_tag=' + I;

    $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){}
    });

    $("#li_"+I).toggleClass("off on");
    element.toggleClass("off on");

    return false;
});
});

Works perfectly. But now I want to add in a second PHP call which will pull data and update another area of the page if the above was successful.

完美的工作。但是现在我想添加第二个PHP调用,如果上面的成功,它将提取数据并更新页面的另一个区域。

What I'm trying to add is:

我想补充的是:

$.ajax({
    url: "_js/loaddata.php",
    success: function(results){
        $('#listresults').empty();
        $('#listresults').append(results);
    }
});

But just adding it into success: function(){} doesn't seem to be working. To clarify, here is the complete code I'm testing:

但只是将其添加到成功:function(){}似乎不起作用。为了澄清,这是我正在测试的完整代码:

$(function() {
$.ajaxSetup ({cache: false});
$(".changetag").click(function(){
    var element = $(this);
    var I = element.attr("id");
    var info = 'switch_tag=' + I;

    $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){
            $.ajax({
                url: "_js/loaddata.php",
                success: function(results){
                    $('#listresults').empty();
                    $('#listresults').append(results);
                }
            });
        }
    });

    $("#li_"+I).toggleClass("off on");
    element.toggleClass("off on");

    return false;
});
});

The PHP scripts are both called successfully and the toggle class works, but the data pulled is not written to #listresults for some reason.

PHP脚本都被成功调用,并且切换类可以工作,但由于某种原因,拉出的数据不会写入#listresults。

1 个解决方案

#1


18  

Ajax calls are (by default) asynchronous. That means that this code:

Ajax调用(默认情况下)是异步的。这意味着这段代码:

$("#li_"+I).toggleClass("off on");
element.toggleClass("off on");

return false;

could be executed before the ajax call preceding it is finished. This is a common problem for programmers who are new to ajax and asynchronous code execution. Anything you want to be executed after the ajax call is done must be put into a callback, such as your success handler:

可以在它之前的ajax调用完成之前执行。对于不熟悉ajax和异步代码执行的程序员来说,这是一个常见问题。完成ajax调用后你想要执行的任何东西都必须放入回调中,例如你的成功处理程序:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");
    }
});

Likewise, you could put the second ajax call in there as well:

同样,你也可以在那里放第二个ajax调用:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");

        $.ajax({
            url: "_js/loaddeals_v2.php",
            success: function(results){
                $('#listresults').empty();
                $('#listresults').append(results);
            }
        });
    }
});

With jQuery 1.5's Deferred Object, you can make this slicker.

使用jQuery 1.5的Deferred Object,你可以使这个更加流畅。

function firstAjax() {
    return $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){
            $("#li_"+I).toggleClass("off on");
            element.toggleClass("off on");
        }
    });
}

// you can simplify this second call and just use $.get()
function secondAjax() {
    return $.get("_js/loaddata.php", function(results){
        $('#listresults').html(results);
    });
}

// do the actual ajax calls
firstAjax().success(secondAjax);

This is nice because it lets you un-nest callbacks - you can write code that executes asynchronously, but is written like synchronously-executed code.

这很好,因为它允许您取消嵌套回调 - 您可以编写异步执行的代码,但是编写为同步执行的代码。

#1


18  

Ajax calls are (by default) asynchronous. That means that this code:

Ajax调用(默认情况下)是异步的。这意味着这段代码:

$("#li_"+I).toggleClass("off on");
element.toggleClass("off on");

return false;

could be executed before the ajax call preceding it is finished. This is a common problem for programmers who are new to ajax and asynchronous code execution. Anything you want to be executed after the ajax call is done must be put into a callback, such as your success handler:

可以在它之前的ajax调用完成之前执行。对于不熟悉ajax和异步代码执行的程序员来说,这是一个常见问题。完成ajax调用后你想要执行的任何东西都必须放入回调中,例如你的成功处理程序:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");
    }
});

Likewise, you could put the second ajax call in there as well:

同样,你也可以在那里放第二个ajax调用:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");

        $.ajax({
            url: "_js/loaddeals_v2.php",
            success: function(results){
                $('#listresults').empty();
                $('#listresults').append(results);
            }
        });
    }
});

With jQuery 1.5's Deferred Object, you can make this slicker.

使用jQuery 1.5的Deferred Object,你可以使这个更加流畅。

function firstAjax() {
    return $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){
            $("#li_"+I).toggleClass("off on");
            element.toggleClass("off on");
        }
    });
}

// you can simplify this second call and just use $.get()
function secondAjax() {
    return $.get("_js/loaddata.php", function(results){
        $('#listresults').html(results);
    });
}

// do the actual ajax calls
firstAjax().success(secondAjax);

This is nice because it lets you un-nest callbacks - you can write code that executes asynchronously, but is written like synchronously-executed code.

这很好,因为它允许您取消嵌套回调 - 您可以编写异步执行的代码,但是编写为同步执行的代码。