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

205
Vistas
Done function never called after $.ajax

I'm a bit new to all this (including Javascript callbacks and ES6). I'm using NodeJS + Express + MongoDB.

I'm calling an Ajax function to update an item and the success Ajax call is never done.

Here is my Ajax call (called from React)

editBug : function(bug){

    console.log('about to edit bug with these values',bug);
    $.ajax({
        url:'/api/bugs',
        method: 'PUT',
        data:bug
    })
    .done((jqxhr) => {
        console.log('succcess while editing the bug');
        this.setState({successVisible : true});
    })
    .fail((jqxhr) => {
        console.log('error : ' + jqxhr);
    })  
},

Here is my API function:

app.put('/api/bugs',function(req,res){

    //console.log('req',req);
    console.log('query string : ',req.query);
    console.log('query params : ',req.params);
    console.log('query body: ',req.body);
    let id = new ObjectID(req.body._id);
    req.body._id = new ObjectID(req.body._id);

    db.collection('bugs').replaceOne(
        {_id:id},
        req.body,
        function(err,result){
            assert.equal(err,null);
            console.log('Successfull replace!');
            res.status(200);
        }
    );
});

The Successfull replace! log is correctly shown on the server side. The about to edit bug with these values is correctly shown on the front side. But the succcess while editing the bug log is not shown on front end and it seems .done call is never executed.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Don't you need to end your response object on the Node.js side? Try adding res.end(); or any kind of response to your response object.

Also, you can use chrome's (or any other browser's) network tab to actually see how your AJAX requests end up, to see if they hang or finish.

about 4 years ago · Santiago Trujillo Denunciar

0

The problem is that you are not sending any response back to the browser on node side. Try the following snippet and you should be good to go

Also, I'd like to point out that you should handle the errors. While updating the bugs if something goes wrong, the best practice would be to inform the browser with the 500 status code indicating that the intended action failed. I've added this aspect in the snipped below

app.put('/api/bugs', function(req, res) {

  //console.log('req',req);
  console.log('query string : ', req.query);
  console.log('query params : ', req.params);
  console.log('query body: ', req.body);
  let id = new ObjectID(req.body._id);
  req.body._id = new ObjectID(req.body._id);

  db.collection('bugs').replaceOne({
      _id: id
    },
    req.body,
    function(err, result) {
      if (err) {
        console.log('Failed replace');
        res.status(500).end(); // <- We set the response status code and end the request
      } else {
        assert.equal(err, null);
        console.log('Successfull replace!');
        res.status(200).end(); // <- We set the response status code and end the request
      }
    }
  );
});
about 4 years ago · Santiago Trujillo 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