' /故事'页面是特定于用户的,需要有效的登录凭据。
在重定向之前,节点,服务器端和正确日志会进行验证,但不会重定向到故事页面。
这似乎是因为" You can't make a redirection after an AJAX. You need to do it yourself in Javascript",但这个问题的第一个答案中的代码似乎有点不完整..对于成功通话..
此外,如何在重定向后成功调用后续函数(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});
}
});
});
答案 0 :(得分:3)
试试这个
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(...)将最好地模拟HTTP重定向 How to redirect to another webpage in JavaScript/jQuery?
对于您的刷新故事'这些东西,你应该只是在去故事'
时刷新故事