简体   繁体   English

使用带有 node.js 和 express 的 jQuery Ajax 来验证发送电子邮件

[英]Using jQuery Ajax with node.js and express to validate sending an email

So, I have this contact form in the website I'm making, I'm using a template I found online that uses ajax to validate that the form is working and the email has been sent, the problem is that this file that validates the form is set to work with PHP I think, so I tried modifying the file that has ajax to make it work with my node app, but I end up with the form loading forever and I get Failed to load resource: net::ERR_EMPTY_RESPONSE in the console.所以,我在我正在制作的网站上有这个联系表格,我正在使用我在网上找到的一个模板,它使用 ajax 来验证表单是否正常工作并且电子邮件已经发送,问题是这个文件验证了我认为表单设置为与 PHP 一起使用,因此我尝试修改具有 ajax 的文件以使其与我的节点应用程序一起使用,但最终表单永远加载并且我无法加载资源:net::ERR_EMPTY_RESPONSE控制台。

note: the form is working and all and the email gets sent but I just need an indication to the user that it worked.注意:表单正在工作,所有内容和电子邮件都已发送,但我只需要向用户表明它有效。

here's the code:这是代码:

validate.js:验证.js:

$.ajax({
  method: "POST",
  url: "/home",
  data: str,
  success: function(msg) {
    if (msg == 'OK') {
      this_form.find('.loading').slideUp();
      this_form.find('.sent-message').slideDown();
      this_form.find("input:not(input[type=submit]), textarea").val('');
    } else {
      this_form.find('.loading').slideUp();
      this_form.find('.error-message').slideDown().html(msg);
    }
  }
})

index.js:索引.js:

const express    = require("express"),
      router     = express.Router(),
      nodemailer = require("nodemailer"),
      mailGun    = require("nodemailer-mailgun-transport");

router.get("/home", (req, res) => {
    res.render("index");
});

router.post("/home", (req, res) => {
    let { firstName, lastName, email, website, company, message } = req.body;
    console.log("Data: ", req.body);

let mailOptions = {
    from: email,
    to: "myemail@email.com",
    subject: "No Subject",
    html:   "<h3>First Name: " + firstName + "</h3>" +
            "<h3>Last Name: " + lastName + "</h3>" +
            "<h3>Email: " + email + "</h3>" +
            "<h3>Company: " + company + "</h3>" +
            "<h3>Website: " + website + "</h3>" +
            "<h3>Message: " + message + "</h3>"
}

smtpTransport.sendMail(mailOptions, (error, response) => {
    if (error) {
        console.log(error)
    } else {
        console.log("Successfully sent email.")
    }
})
});

The Error says: Error Empty Response .错误说: Error Empty Response 。

So why don't you just send a response ?那么你为什么不直接发送响应呢?

router.post("/home", (req, res) => {
    let { firstName, lastName, email, website, company, message } = req.body;
    console.log("Data: ", req.body);

let mailOptions = {
    from: email,
    to: "myemail@email.com",
    subject: "No Subject",
    html:   "<h3>First Name: " + firstName + "</h3>" +
            "<h3>Last Name: " + lastName + "</h3>" +
            "<h3>Email: " + email + "</h3>" +
            "<h3>Company: " + company + "</h3>" +
            "<h3>Website: " + website + "</h3>" +
            "<h3>Message: " + message + "</h3>"
}

smtpTransport.sendMail(mailOptions, (error, response) => {
    if (error) {
        console.log(error);
        res.status(500).send('false');  // <----- HERE
    } else {
        console.log("Successfully sent email.");
        res.send("OK");   // <------------- HERE
    }
})
});

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

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