Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!