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

211
Visualizações
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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório

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
      }
    }
  );
});
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