Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple AJAX Get request is "pending"

I am trying to implement a simple AJAX GET request. This request tells the server to delete a document in a database. While I get confirmation from the server that the document is deleted, the Chrome Inspector shows that the request sits as "pending", eventually resulting in a server error.

What am I doing incorrectly?

HTML

<div class="delete-note">
    <a href="#"><i class="icon-minus-sign"></i></a>
</div>

JS

$('.delete-note').click(function(e) {
  var url = '/docs/' + doc_id + '/note_destroy/' + note_id;
  $.ajax({
    type: "GET",
    url: url,
    cache: false,
  });
  e.preventDefault();
});

Edit: Including server-side code as well:

Node/Express

exports.note_destroy = function(req, res) {
  Doc.findOne({ doc_id : req.params.doc_id }, function(err, data) {
    if (err) throw (err);
    note_id = req.params.note_id;
    data.notes.id(note_id).remove();
    data.save(function(err) {
      if (err) throw (err);
      console.log('note ' + note_id + 'is removed.');
    });
  });
};
like image 939
Evan Emolo Avatar asked Apr 29 '13 17:04

Evan Emolo


1 Answers

Your server-side function doesn't write any response, so the browser waits for it indefinitely. Try writing a response, perhaps with some kind of status code that the client-side can check to tell whether the deletion worked. Something like...

res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ status: OK }));
res.end();
like image 62
Martin Avatar answered Nov 17 '22 12:11

Martin