Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Why does this client side $.get() request only run the server side code successfully?

I have created a contact form that sends a get request to a route I have hosted locally from a node(express) server. I send the get request when an element is clicked on the dom.

($(document).ready(function () {
  $('#sendEmail').click((e) => {
    e.preventDefault();

    let name = $('#name').val();
    let email = $('#email').val();

    $.get("http://localhost:3000/send", {name: name, email: email})
    .done(() => {
        alert('success!');
    })
    .fail(() => {
        alert('That did not work');
    })
})($)

The node server generates an email based off of information that is gathered from the client. The good news is that the email that is generated works perfectly, and is emailed to my gmail account using nodemailer.

app.get('/send', (req, res) => {
  const mailOptions = {
    name: req.query.name,
    email: req.query.email,
    message: req.query.message,
    to: "",
    from: req.query.name,
    text: ""
  }

  smtpTransport.sendMail(mailOptions, (error, response) => {
    if (error) {
      console.log(error);
      response.end('error');
    } else {
      console.log('Email sent!');
      response.end('sent')
    }
  })
})

Everything is successful server side but no client side code runs after that. Neither my $.done() function or my $.fail() function end up running. What do I need to do so that I can run client side code after the email sends (or doesn't send)?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Because you don't response the result to the client.
You need call 'res' instead of 'response' in the callback of 'sendMail':

app.get('/send', (req, res) => {
  const mailOptions = {
    name: req.query.name,
    email: req.query.email,
    message: req.query.message,
    to: "",
    from: req.query.name,
    text: ""
  }

  smtpTransport.sendMail(mailOptions, (error, response) => {
    if (error) {
      console.log(error);

      //response.end('error');
      res.end('error'); // <=====================

    } else {
      console.log('Email sent!');
      //response.end('sent')
      res.end('Email sent!'); // <=====================
    }
  })
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!