0

Goal: I want to use a 'post' method to send a http request to another website (ignore data), and then redirect to another page. Like this,

    $.when($.post("https://www.example.com/result") )
    .then( $(location).href('page2.html'));

It seems like the redirect process works fine, but not the post process.

I'm sure that my post method is correct, when it stands alone.

But when I put it with the redirect method. It does not work.

I assume that $(location) has been executed while the post method still does not finish yet.

What should I do?

Thank you

1 Answers1

2

.then() takes a callback. You're passing a function and executing it. Simply wrap it in a function:

$.when( $.post("https://www.example.com/result") )
 .then(function (data, textStatus, jqXHR) { 
    $(location).href('page2.html') 
});

Note the parameters passed to the callback, as described in jQuerys API.

As jfriend00 suggested in the comments, a simpler solution to your problem would be to use the .done() function:

$.post("https://www.example.com/result").done(function () {
    $(location).href('page2.html');
});
Frederik Wordenskjold
  • 10,031
  • 6
  • 38
  • 57