0

I have created an AJAX request, which after it is done it redirects the page. It ignores the response.

I am trying to get it to make sure it has finished sending the POST before it redirects. Everything I find online has how to get it to trigger something after it has a success or failure. I don't want to wait that long. Is there a way to get it to just wait till it has sent the POST then do something?

$.ajax({
    data: "type=userLoginLog&username=" + username, //Post Username
    url: environmentalVariables.support_api_address
});
loginWithRefreshToken(refresh_token);
AndrewL
  • 334
  • 2
  • 13

3 Answers3

1

No, there is no way to know exactly when it has sent. The only notification you get from Ajax is when the response or error is received or the request timed out. There is no "sent now" notification.

You could experiment and do the redirect on a timer (trying to find the shortest timing that made sure the request was sent), but that would be a bit of a hack and could easily vary from one browser to another.

The best solution would be for the server receiving the ajax call to return an immediate response (even if it isn't done with its own processing yet) and then you could just trigger the redirect upon receiving the response like this:

$.ajax({
    data: "type=userLoginLog&username=" + username, //Post Username
    url: environmentalVariables.support_api_address
}).then(function() {
    loginWithRefreshToken(refresh_token);
});

If the server is implemented properly, this should take only a smallish number of milliseconds to get the response.


You could even combine the two schemes above and trigger the redirect on the first action that completes (a timer or the ajax response):

function delay(x) {
    return new Promise(function(resolve) {
        setTimeout(resolve, x);
    });
}

var p = $.ajax({
    data: "type=userLoginLog&username=" + username, //Post Username
    url: environmentalVariables.support_api_address
});

// you will have to figure out what delay time is appropriate here
$.when(p, delay(200)).then(function() {
    loginWithRefreshToken(refresh_token);
})
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That is what I was concerned it would be, the server is PHP, so it is harder to make it send back a response then keep processing. – AndrewL Oct 14 '16 at 02:23
  • @AndrewL - See [Continue processing php after sending response](http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response). – jfriend00 Oct 14 '16 at 02:24
  • Thanks, I think I will use your second solution, with changing the server to respond straight away as well. Thank you. – AndrewL Oct 14 '16 at 02:48
0
$.ajax({
    url: "post_url",
    type: 'post',
    data: dataObj,
    datatype: 'application/json',
    beforeSend: function() {
        // do something
    },
    complete: function() {
        // do something
    },
    success: function(response) {
        window.location.href= "_success_page_url";
    },
    error: function(jqXMLHttpRequest, textStatus, errorThrown) {
        window.location.href= "_error_page_url";
    }
});
  • That does on response, I am not wanting to wait for the response. – AndrewL Oct 14 '16 at 02:17
  • why don't you want to wait for the response? ajax is an async method, if you don't care the response, you can call other method after it but you don't have any response data ? – Hoàng Trần Oct 14 '16 at 02:22
  • The next function redirects the page, so if the AJAX hasn't finished sending the browser cancels it. – AndrewL Oct 14 '16 at 02:24
  • if your requesting arrives, you only lose response data, server still does your post. But you never know when it arrives. settimeout or other is almost right – Hoàng Trần Oct 14 '16 at 02:37
0
$.ajax({
    data: "type=userLoginLog&username=" + username, //Post Username
    url: environmentalVariables.support_api_address
}).done(function(){
    loginWithRefreshToken(refresh_token);
});
Bob Dust
  • 2,370
  • 1
  • 17
  • 13