As someone new to AJAX and Node, I encountered a dilemma that I hope to get some guidance on. Here's the issue: I have a DELETE ajax call that removes a row from the database and I want to redirect back to the same route with a GET method afterwards. However, I'm unsure whether I should handle the redirect from the front-end script or using another strategy. Any advice would be greatly appreciated!
Front-end JavaScript for making the Ajax call (script.js)
$('.deleteRepository').on('click', function(event) {
event.stopImmediatePropagation();
var username = $(this).attr('username');
var repoName = $(this).attr('repoName');
var oReq = new XMLHttpRequest();
oReq.open("delete", "/user/"+username+'/'+repoName);
oReq.send();
});
myRoute.js
router.route('/:username/:repository')
.delete(function(req, res) {
console.log('\n\nDelete method called\n\n');
let username = req.params.username;
let repoName = req.params.repository;
deleteRepositoryFromUser(req, res)
.then(function() {
console.log('deleted, now redirect');
//I want to redirect to this page using GET, but logs DELETE
//I also want to redirect to /user/TonyStark/Favorites
res.redirect(200, '/user/'+username+'/Favorites');
})
Console Output
GET /user/TonyStark/delMe 200 114.702 ms - 4075
...
Delete method called
deleteRepositoryFromUser()
repoName: delMe
username: TonyStark
deleted, now redirect
DELETE /user/TonyStark/delMe 200 4.568 ms - 44 //how can I make GET?
//why is it not going to /user/TonyStark/Favorites?