node.js - Expressjs - AJAX not recognized by req.xhr -
i have action within application user triggers delete
ajax call delete account , redirected homepage message. action occurs expected, message not appear. can't use flash messages because destroying session on deletion, decided use if(req.xhr)
, pass object view render if true
, view if false
. however, doesn't appear route recognizing x-requested-with:xmlhttprequest
delete
, delivering else
statement. can point me why might be?
here route:
siteroutes.get('/', function(req, res) { if(req.xhr) { console.log("ajax request") res.render('pages/site/index.hbs',{ successmessage: "account deleted." }); } else { console.log("regular request") //being triggered res.render('pages/site/index.hbs'); } });
here ajax:
$.ajax({ method: 'delete', url: '/app/settings/account/delete', data: { successmessage: "account deleted" }, success: function(){ window.location.replace('/'); }, error: function(error){ console.log(error); } });
update:
approutes.delete('/settings/account/delete', function(req, res){ return .... .then(function(){ req.session.destroy(); req.logout(); })
your ajax function send method delete
:
$.ajax({ method: 'delete', url: '/app/settings/account/delete', data: { successmessage: "account deleted" }, success: function(){ window.location.replace('/'); }, error: function(error){ console.log(error); } });
so should change route method delete
this:
siteroutes.delete('/', function(req, res) { if(req.xhr) { // sure works console.log("ajax request") res.render('pages/site/index.hbs',{ successmessage: "account deleted." }); } else { console.log("regular request") //being triggered res.render('pages/site/index.hbs'); } });
Comments
Post a Comment