Troy Cosentino
Troy Cosentino

Reputation: 4778

wait to leave page until ajax is done

Basically, I am sending a $.POST() when an element is clicked. this element brings the user to a different page, and the POST request is not going all the way through. If i step through it slowly the script gets ran, however.

How can i make the page wait until the script has run until it navigates away from the page? or is there a different issue here? Sort of new to AJAX

$('.ad-link').on('click', function() {
    $.get('http://easyuniv.com/php/clickPP.php?id='+$(this).attr('id'));
});

and then clickPP.php is just a simple SQL query that i have verified to work.

Thanks

Upvotes: 0

Views: 201

Answers (1)

Abhilash
Abhilash

Reputation: 1610

You can use a callback for this, like so:

$('.ad-link').on('click', function(e) {
    e.preventDefault();
    var url_to_redirect = $(this).attr('href');
    $.get(
        'http://easyuniv.com/php/clickPP.php?id='+$(this).attr('id'),
        {},
        function(return_data){  // callback function, wait till request is finished
            // use the return_data if you need it
            location.href = url_to_redirect; // redirect the user
        }
    );
});

Upvotes: 2

Related Questions