AJAX request after other AJAX requests

Asked

Viewed 435 times

2

I need to make an AJAX request after any other AJAX request.

However, using the method $(document).ajaxComplete(), for example, the request would end in an infinite loop.

Is there any easier way to make this request without calling it after each of the others?

What this request does is pull backgrounds from the logged-in user in a database and display them in an HTML page.

Whenever the user modifies one of its issues, it is updated via AJAX as well, but old issues continue to appear on the page.

So I need that after each other request this be executed also to update the user backlog.

The request I want to execute is very simple:

function sucesso(data, textStatus, jqXHR) {
    var htmlPendencias = jqXHR.responseText;
    $("#div-pendencias").text("");
    $("#div-pendencias").append(htmlPendencias);
}
var conf = {
    method: "post",
    data: {opcao: 2},
    success: sucesso,
    dataType: "text"
};
$.ajax("logado", conf);

1 answer

0


You can use the configuration property global.

It has the function of controlling if an ajax request triggers internal events, which in turn triggers the global callback .ajaxComplete.

So does the call that is always used:

var conf = {
    method: "post",
    data: {opcao: 2},
    success: sucesso,
    dataType: "text",
    global: false // <-----
};

You can see an example of this here: http://jsfiddle.net/a5u7mxm0/

And notice that in global callback, the third comparison is never true, and that callback is only called twice. The example code is:

function dummie(name) {
    return {
        url: '/echo/js/?js=hello%20world!',
        complete: function(response) {
            console.log(response.responseText, name);
        }
    };
}

var a = $.ajax(dummie('A'));
var b = $.ajax(dummie('B'));
var specialDummie = dummie('XPTO');
var xpto = $.ajax((specialDummie.global = false, specialDummie));

$(document).ajaxComplete(function(event, jqXHR, ajaxOptions) {
    console.log(jqXHR == a, jqXHR == b, jqXHR == xpto);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.