AJAX request after other AJAX requests

2

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

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

Is there an easier way to do this request without calling it after each other?

What this request does is to pull pending user from a database and display it on an HTML page.

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

This is why I need to do this after every other request to update the pending issues as well.

The request I want to execute is pretty 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);
    
asked by anonymous 09.08.2016 / 18:54

1 answer

0

You can use the global setting property.

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

So it does in the call that is always used:

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

You can see an example here: link

And notice that in global callback, the third comparison is never true , and that the callback is only called 2 times. The code for the example 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);
});
    
09.08.2016 / 21:16