简体   繁体   中英

jQuery.post() send email but doesn't gets to .done()/.fail()/.always() without server refresh

I'm trying to basically just send a form information to e-mail without having to refresh the page and I came all the way to this. The issue here is, that even though it sends the email correctly, it doesn't clear the form because the $.post() never gets to the .done() or .fail() or .always(). Although I noticed that as I work with nodemon when I change the code and save it, it refreshes the server and then it gets to .fail() and .always() but never to .done().

do you guys know what should I do to make the $.post() get correctly to done/fail/always, please?

code looks like this:

HTML

<form class="trial-form" action="/trial-submission" method="post">
      <input id="company" type="text" name="company" placeholder="Company" />
      <input id="person" type="text" name="person" placeholder="Contact person" />
      <input id="phone" type="tel" name="telephone" placeholder="Phone" />
      <input id="email" type="email" name="mailAddress" placeholder="E-mail" />
      <input id="note" type="text" name="note" placeholder="Note" />
      <button class="blue-button" id="submit-trial" type="button">send</button>
</form>

JavaScript

$(document).ready(function(){
  $("#submit-trial").click(function(event){
    let cmpn = $("#company").val();
    let eml = $("#email").val();
    let phn = $("#phone").val();
    let nt = $("#note").val();
    let prsn = $("#person").val();
    $.post("/trial-submission", {
      company: cmpn,
      person: prsn,
      phone: phn,
      email: eml,
      note: nt
    })
    .done(function(){
      $("#company").val("");
      $("#email").val("");
      $("#phone").val("");
      $("#note").val("");
      $("#person").val("");
      alert("success");
    })
    .fail(function(){
      alert("error");
    })
    .always(function(){
      alert("finished");
    });
  });
});

and the node stuff is here

app.post("/trial-submission", function(req, res){
  const data = req.body;

  let transporter = nodemailer.createTransport({
       service: "mail",
       auth: {
         user: "send@mail.com",
         pass: "password"
       }
  });

  let mailOptions = {
    from:'send@mail.com',
    to:'receive@mail.com',
    subject: 'testing',
    html:`
      Company: ${data.company}<br>
      Contact person: ${data.person}<br>
      Phone: ${data.phone}<br>
      E-mail: ${data.email}<br>
      Note: ${data.note}
    `
  };

  transporter.sendMail(mailOptions, function(err, data){
    if(err){
      console.log('Error Occurs', err);
    } else {
      console.log('Email Sent!');
    }
  });
});

You need to send a response in your sendMail call, otherwise your browser never receives anything. Try this:

transporter.sendMail(mailOptions, function(err, data){
    if(err){
      console.log('Error Occurs', err);
      res.sendStatus(400);
    } else {
      console.log('Email Sent!');
      res.sendStatus(204);
    }
  });

The 400 is a simple "Bad Request", and the 204 is "Success/No Content". You can modify these further, but for now the responses will be received by the client so you can proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM