Node.js页面redirect在AJAXlogin? redirect后有函数调用吗?

“/故事”页面是用户特定的,需要有效的login凭据。

validation是由节点,服务器端,并在redirect之前正确logging,但不redirect到故事页面。

这似乎是因为“ 你不能在AJAX之后进行redirect,你需要用Javascript自己来做 ”,但是这个问题的第一个答案中的代码似乎有些不完整。

另外,如何在redirect后成功调用后续函数(refreshStories)?

这里是AJAX:

if (loginUsername.length != 0) { console.log('there is a login: ' + loginUsername); // make an ajax 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 a user (accessed at 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 will know the user is 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 to stories'); } else { console.log('user authentication badCredentials error..'); res.render('index', {badCredentials: true}); } }); }); 

尝试这个

 if (loginUsername.length != 0) { console.log('there is a login: ' + loginUsername); // make an ajax 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(…)将最好地模拟HTTPredirect如何redirect到JavaScript / jQuery中的另一个网页?

对于你的'refreshstories'的东西,你应该只是刷新故事,当你去'/故事'