Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda