简体   繁体   English

jQuery.post() 发送电子邮件但不会在没有服务器刷新的情况下到达 .done()/.fail()/.always()

[英]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().这里的问题是,即使它正确发送了电子邮件,它也不会清除表单,因为 $.post() 永远不会到达 .done() 或 .fail() 或 .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().尽管我注意到当我更改代码并保存它时使用 nodemon 时,它会刷新服务器,然后它会到达 .fail() 和 .always(),但永远不会到达 .done()。

do you guys know what should I do to make the $.post() get correctly to done/fail/always, please?你们知道我应该怎么做才能使 $.post() 正确完成/失败/总是吗?

code looks like this:代码如下所示:

HTML 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 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.你需要在你的 sendMail 调用中发送一个响应,否则你的浏览器永远不会收到任何东西。 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". 400 是一个简单的“错误请求”,而 204 是“成功/无内容”。 You can modify these further, but for now the responses will be received by the client so you can proceed.您可以进一步修改这些,但现在客户端将收到响应,因此您可以继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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