Jump to content

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

Go to solution Solved by Person_125,

I solved it by sending a response in my index.js file like so:

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
    }
})
});

 

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.

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:

$.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:

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.")
    }
})
});

Thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

I solved it by sending a response in my index.js file like so:

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
    }
})
});

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×