編輯:網址是罪魁禍首,我認爲。j_security_check阿賈克斯
FINE:安全檢查請求POST/SesamaMaven /保護/管理/ j_security_check
而在AJAX版本我:
FINE:安全檢查要求在工作login.html的情況下,我在日誌了POST/SesamaMaven/
我在GlassFish中配置的認證與JDBCRealm,它似乎與普通的login.html這樣的合作:
<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>
我的問題是,當我試圖實現相同的AJAX,它不工作。是否有可能得到這方面的工作?
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);
}
});
});
質詢
1)什麼是正確的contentType: 「應用程序/文本」?
2)URL標記是正確的還是應該使用操作?
3)如果是這樣的情況下,參數username和password怎麼樣?
Glassfish嘗試進行身份驗證,但沒有用戶和密碼。
舊的,所以只針對那些感興趣的人:1)您需要先登錄安全頁面,然後才能登錄(容器需要啓動登錄過程)。 2。失敗登錄的響應是重定向到錯誤URL,因此您需要在ajax響應中檢查這一點。 3. Content-Type應該是'x-www-form-urlencoded'(普通的HTML表單文章) –