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

236
Visualizações
Communication between inline JavaScript and Node.js

I am using Node.js and I want to send a request which contains an input value to the server when a button is pressed.

So here's my HTML code :

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>

    <body>
        <input id="input" action="/onclick" autocomplete="off">
        <button id="button">Send</button>
        <script>
            let input = document.getElementById('input');

            document.getElementById('button').addEventListener('click', (event) => {
                event.preventDefault();
                let value = input.value;

                if (value) {
                    let data = {};
                    data.title = 'getValue',
                    data.message = value;

                    $.ajax({
                        url:'/onclick',
                        data: JSON.stringify(data),
                        type: 'POST',
                        success: (responseData) => {
                            console.log('Success!');
                        },
                    });

                    input.value = null;
                }
            });
        </script>
    </body>
</html>

And here's my server.js :

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

app.post('/onclick', (req, res) => {
    console.log('body: '+ JSON.stringify(req.message));
    res.send(req.message);
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

The code doesn't throw any error (it returns Success! in browser console) but the problem is that in the Node.js shell it sends body: undefined instead of the input value.

Do you know how to fix that ?

Thanks in advance,

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add this line in your server.js to access the request body:

app.use(express.json());

And use req.body instead of req.message to access the request body

about 4 years ago · Juan Pablo Isaza 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