0

Okay, so it could probably be me missing something important, but as far as I know AJAX is asynchronous.

When a user registers for my Website, he shall be directed to a success-site while in the background a registration-mail is sent.

The code I came up with looks as follows:

$.ajax({
    cache: false,
    async: true,
    type: "POST",
    url: "/Account/SendRegistrationMail",
    data: { //someDataNeededByController},
    success: function() {
                  console.log("fire and ice");
              }
    });
window.location.href = "/Account/RegistrierenErfolgreich";

Nevertheless the user is being redirected after the mail has been sent: https://i.stack.imgur.com/h1O8A.png

Rustius
  • 173
  • 1
  • 4
  • 14
  • Tbh I don't see why this is a duplicate. The window.location.href is outside of my AJAX, so as far as I understand everything I read in the linked question, this should redirect while the POST is still in execution. – Rustius Aug 25 '17 at 08:34
  • 2
    You can't be sending the POST request and redirect to another page at the same time: you will have to wait for it, otherwise the sending will be terminated. See: https://stackoverflow.com/questions/32544141/window-location-seems-to-cancel-ajax-post-request – Terry Aug 25 '17 at 08:38
  • what's the point of even using ajax for this, if you're just going to redirect immediately anyway? Just submit the form normally, trigger the email send operation, and redirect the user to the success page from there. – ADyson Aug 25 '17 at 08:53
  • Okay, thank you :) – Rustius Aug 25 '17 at 08:56
  • The point is that due to problems with the SMTP-Server waiting for the email to be sent took up to a minute, which is not a very good user experience. – Rustius Aug 25 '17 at 08:57
  • in that case your web app could just write the information into a table, and another automated background service can read it from that table and send the emails, so that the timing is less of a problem and the web app is not impacted. Most large scale applications hand off tasks like this to background services or workers to avoid disrupting the UI with things like timing issues. – ADyson Aug 25 '17 at 09:26

0 Answers0