Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
req.body isn't picking the data from the form which is being sent through the addingTask() function to the url : '/newtask'
Here is the form and addingTask() function.
<form method="GET" action="/newtask" onsubmit="addingTask()">
  <label> Today's Task: </label><br>
  <input type="text" id="task"><br>
  <button type="submit">Add</button>
</form>
<script>
  function addingTask() {
    var data = {
      task: $("#task").val(),
    }
    $.ajax({
      type: "GET",
      url: "/newtask",
      dataType: 'JSON',
      data: data,
      success: function success(result) {
        console.log("iiiiiiiiiiiii", result);
        console.log("565656565656565");
      },
      error: function error(error) {
        console.log("sdsdsdsdsd", error);
        console.log("mmmmnnnnmmmnnn");
      }
    })
  }
</script>
   This is the route file.
   var express = require('express');
   var router = express.Router();
   const indexController = 
   require("../controllers/indexController");

   /* GET home page. */
   router.get('/', function(req, res, next) {
   res.render('index', { title: 'To-Do-List' });
   });

   router.get('/newtask', indexController.createTask
   );

   module.exports = router;
   This is the controller function file.
   
   const createTask = (req, res, next) => {
   console.log(req.body);
   var variable = JSON.stringify(req.body);
   res.render('index', { title: variable });
   };

   module.exports = {          //A module means file
   createTask
   }

Initial Screen https://i.stack.imgur.com/itWPX.png

After filling the form and on clicking the add button i get empty title https://i.stack.imgur.com/AqIWf.png

I have tried changing http method to 'POST' but still the same error occurs.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The "type" key inside $.ajax() should be 'POST' and also in the router file it should be router.post and finally, inside controller function file it should be res.send({data: req.body.task}); instead of res.render(index, { title: variable });

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda