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.');
});
});
};
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With