javascript - Node.js Page Redirect on AJAX login? With a function call after redirect? -
the '/stories' page user-specific , requires valid login credentials.
validation accounted node, server-side, , logs before redirect, not redirect stories page..
this seems because "you can't create redirection after ajax. need in javascript", code in first reply question seems incomplete..for succcess call..
also, how can subsequent function (refreshstories) called on success after redirect?
here's ajax:
if (loginusername.length != 0) { console.log('there login: ' + loginusername); // create ajax phone call $.ajax({ datatype: 'json', data: ajaxlogindata, type: 'post', url:"http://localhost:4200/api/v1/users", success: (data, textstatus, jqxhr) -> if typeof data.redirect == 'string' window.location = data.redirect success: refreshstories, error: founderror });
node.js + express4
router.route('/users') // log in user (accessed @ post http://localhost:4200/api/v1/users) .post(function(req, res) { var username = req.body.loginusername; var password = req.body.loginpassword; authenticateuser(username, password, function(err, user){ console.log('authenticate user..'); if (user) { console.log('yes user'); // subsequent requests know user logged in req.session.username = user.username; console.log('set session username to: ' + req.session.username); // res.redirect('/stories'); res.send({redirect: '/stories'}); console.log('user logged in .. redirect stories'); } else { console.log('user authentication badcredentials error..'); res.render('index', {badcredentials: true}); } }); });
try this
if (loginusername.length != 0) { console.log('there login: ' + loginusername); // create ajax phone call $.ajax({ datatype: 'json', data: ajaxlogindata, type: 'post', url:"http://localhost:4200/api/v1/users", success: (data, textstatus, jqxhr) -> if (typeof data.redirect == 'string') window.location.replace(window.location.protocol + "//" + window.location.host + data.redirect); error: founderror });
window.location.replace(...) best simulate http redirect how can create redirect page in jquery/javascript?
for 'refreshstories' stuff, should refresh stories when go '/stories'
javascript ajax node.js redirect express
No comments:
Post a Comment