res.redirect from POST

You can’t make a redirection after an AJAX. You need to do it yourself in Javascript.

server

post: function(req, res) {
     var login = req.body['login'];          
     app.use(express.bodyParser());


     if (login && req.body['login']['password'] == "tom") {
        var loginPassword = req.body['login']['password'];
        console.log(loginPassword);
        console.log('Granted access');
        res.send({redirect: '/blog'});

     }

     ...

}

client

$(document).ready ->
    $('#login-button').click () ->
        $.ajax
            url: '/login'
            type: 'POST'
            data: $('#Password').serialize()
            dataType: 'json'
            success: (data, textStatus, jqXHR) ->
                if typeof data.redirect == 'string'
                    window.location = data.redirect

This should work.

Leave a Comment