I am using node and trying to redirect a client after he authorizes himself. For this reason I am using a POST method using ajax on the client side which has the form:
$.ajax({
type: "POST",
url: '/login',
dataType: "json",
async: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(credentials.username + ":" + credentials.password));
},
data: credentials,
success: function () {
window.alert("Success! (whatever that means)");
}
});
Then on the server side I am trying to redirect to the actual page with the commands
app.get('/login', loginInternalApp);
app.post('/login', function (req, res){
var postContents = req.body;
req.session.username = postContents.username;
res.redirect('/my_secret_page');
});
app.get('/my_secret_page', checkAuth, function (req, res) {
console.log("sending the message!");
res.send('if you are viewing this page it means you are logged in');
res.end();
});
where checkAuth was taken from here how to implement login auth in node.js (instead of user_id I am using username; that's not the problem).
Perhaps what I do not know is how to treat this ajax call correctly. I print some console messages and the server goes all the way to the res.send('if you are viewing this page...') however, nothing happens on the client. Then when I press a control-C to terminate the server, the alert window pops up. Meanwhile, I can see that the function passed on success can have parameters (guessing now: errorCode, errorString, otherData, perhaps more).
So what am I doing wrong?