2013-05-29 171 views
1

編輯:網址是罪魁禍首,我認爲。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嘗試進行身份驗證,但沒有用戶和密碼。

+0

舊的,所以只針對那些感興趣的人:1)您需要先登錄安全頁面,然後才能登錄(容器需要啓動登錄過程)。 2。失敗登錄的響應是重定向到錯誤URL,因此您需要在ajax響應中檢查這一點。 3. Content-Type應該是'x-www-form-urlencoded'(普通的HTML表單文章) –

回答

0

contentType:「應用程序/文本」是罪魁禍首。我只是評論說,線路輸出,一切都開始工作。

的一個問題仍然存在。當在認證錯誤,但它重定向到index.html的,但沒有CSS和地址欄包括在它應該在的情況下得手去/protected/adminWelcome.html的地址。

0

我將這些代碼添加到login.html文件中。

在發展過程中,我覺得懶得輸入用戶名和密碼

xhttp.open("POST", "j_security_check", true); 
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xhttp.send("j_username=MY_USERNAME&j_password=MY_PASSWORD"); 
location.reload(true); 

看來,你在嘗試,當他們輸入錯誤的憑據INFOR用戶。

在我的情況,我有的login.html錯誤的login.html一模一樣,

除了錯誤的login.html具有文本「你輸入錯誤的密碼或用戶名