user1386320
user1386320

Reputation:

Will an asynchronus-Ajax request be completed if I instantly reload or close my page after it's being sent to the server?

For example if I do this request with jQuery's Ajax:

$(document).ready(function() {
    $('#some_button').click(function() {
        $.ajax({
            url: '/some/request',
            type: 'POST',
            data: [{my: 'dummy', data: 'lata'}],
            dataType: 'json',
            async: true,
        });

        window.location.href = '/my/parent/location';
    });
});

So I'm interested:

Will my action be completed on the server nevermind if I'm refreshing the page just right after the initial request is sent?

Upvotes: 1

Views: 133

Answers (1)

Sushanth --
Sushanth --

Reputation: 55740

Ajax is synchronous.. So it will be immediately redirected irrespective of the Request..

You can call that in the success callback of your Ajax Request instead..

$(document).ready(function() {
    $('#some_button').click(function() {
        $.ajax({
            url: '/some/request',
            type: 'POST',
            data: {my: 'dummy', data: 'lata'},
            dataType: 'json',
            success : function() {
                window.location.href = '/my/parent/location';
            }
        });

    });
});

Upvotes: 1

Related Questions