Polarize
Polarize

Reputation: 1035

POST request on Ajax Complete

I am trying to do a token exchange after every AJAX request performed on my site. To do this, I am currently using the jQuery function .ajaxSuccess. My problem is, whenever I try to perform an AJAX request within this function, it's obviously seeing it as a success and is this creating a recurring function.

How can I make a one-time AJAX request situated within this function which only runs once?

I.e. like so:-

$(document).ajaxSuccess(function() {
    $.post("somewhere", {some: "data"}, function(data){
        console.log(data);
    });
});

Upvotes: 0

Views: 388

Answers (1)

gre_gor
gre_gor

Reputation: 6813

You can check, if you actually need to make an AJAX request in your ajaxSuccess handler like so

$(document).ajaxSuccess(function(event, xhr, settings) {
    if (settings.url != "somewhere") {
        $.post("somewhere", {some: "data"}, function(data){
            console.log(data);
        });
    }
});

Upvotes: 2

Related Questions