UPDATE: It seems like the culprit could be the URL. When using the login.html page, I observed the following log entry:
FINE: Security checking request POST /SesamaMaven/protected/admin/j_security_check
However, in the AJAX version, the log shows:
FINE: Security checking request POST /SesamaMaven/
I have configured authentication in Glassfish with JDBCRealm and it functions properly with a standard login.html form like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Form</title>
</head>
<body>
<form method="post" action="j_security_check">
<p>You need to log in to access protected information.</p>
<table>
<tr>
<td>User name:</td>
<td><input type="text" name="j_username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password" /></td>
</tr>
</table>
<p><input type="submit" value="Login" /></p>
</form>
</body>
</html>
My dilemma arises when attempting to implement the same functionality with AJAX, as it does not seem to work. Is there a way to resolve this issue?
HTML
<form class="navbar-form pull-right">
<input class="span2" type="text" placeholder="Email" name="j_username" id="username">
<input class="span2" type="password" placeholder="Password" name="j_password" id="password">
<button type="button" class="btn" id="btnSignIn">Sign in</button>
</form>
JS
$('#btnSignIn').click(function() {
$.ajax({
type: "POST",
contentType: "application/text",
url: "j_security_check",
// This is the type what you are waiting back from the server
dataType: "text",
async: false,
crossDomain: false,
data: {
j_username: "admin",
j_password: "paSSWORD"
},
success: function(data, textStatus, xhr) {
alert('Thanks for your signin in! ' + xhr.status);
window.location = "/SesamaMaven/protected/adminWelcome.html";
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
window.location = "/SesamaMaven/index.html";
alert(' Error in signIn-process!! ' + textStatus);
}
});
});
QUERIES
1) Is the correct contentType: "application/text" specified?
2) Should the URL tag be accurate or should I use action instead?
3) What about the parameters username and password in this scenario?
Glassfish attempts to authenticate but fails due to missing user and password details.