2013-10-12 64 views
0

我有一個問題,一個成功的Ajax POST(200)請求j_spring_security_check沒有正確重定向到一個success.jsp頁面 - 這是在同一目錄中登錄頁面。 login.jsp頁面只是重新顯示 - 我不明白爲什麼(請注意,我對ajax相當陌生)。成功的Ajax後彈簧安全不重定向

以下是我的login.jsp頁面的內容。任何幫助將大大收到:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
    <head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    <%--<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>--%> 
    <title>Money Tracker - Logon</title> 
    </head> 

    <body> 
     <div> 
     <header> 
     </header> 
     <section> 
      <div id="dialog" title="Login"> 
      <form id="login"> 
       <label for="userName">User:</label> 
       <input id="userName" type="text" name="userName"> 
       <br> 
       <label for="password">Password:</label> 
       <input id="password" type="password" name="password"> 
       <br> 
       <label for="rememberMe">Remember me</label> 
       <input id="rememberMe" type="checkbox" name="rememberMe"/> 
       <br> 
       <button type="submit">Login</button> 
      </form> 
     </div> 
    </section> 
</div> 
<script> 

function ajaxLogin(form) { 
    var userName = form.userName.value; 
    var password = form.password.value; 
    var rememberMe = form.rememberMe.value; 
    $.ajax({ 
     cache: false, 
     type: 'POST', 
     url: "/server/j_spring_security_check", 
     crossDomain: true, 
     async: false, 
     data: { j_username: userName, j_password: password, _spring_security_remember_me: rememberMe }, 
     beforeSend: function (xhr) { 
      xhr.setRequestHeader("x-ajax-call", "true"); 
     }, 
     success: function (result, xhr) { 
      alert("Request was success. Status is: " + xhr.status); 
      if (result == "ok") { 
       alert("Result was OK. Status is: " + xhr.status); 
       window.location.href = "success.jsp"; 
      } else { 
       alert("Result was not OK. Status is: " + xhr.status); 
       location.href = "loginFailed.jsp"; 
      } 

     }, 
     error: function (xhr) { 
      alert("Error with request. Status is: " + xhr.status); 
      location.href = "loginFailed.jsp" 
     }, 
     complete: function(result) { 
     } 
    }); 
} 

$(function() { 
    $("#login").submit(function() { 
     ajaxLogin(this) 
    }); 
}); 
</script> 
</body> 
</html> 
+0

ajax請求不會重定向父頁面。而不是ajax請求使用正常的表單提交 –

+0

您好coding_idiot,我只需要提交登錄請求的形式?正如我向Vinoth提到的那樣,這只是一個輕量級,無狀態,基於REST的服務器。所以客戶端可以是任何東西 - 我需要一個通用的登錄方式。 – Ruaghain

+0

你試過Google搜索[Rest Authentication](https://www.google.co.in/search?q=rest+authentication&oq=rest+ authentication&aqs = chrome.0.69i59j0l5.1870j0j9&sourceid = chrome&espv = 210&es_sm = 122&ie = UTF-8) –

回答

0

@Ruaghain我有一個建議給你。 Ajax的目的不是提交整個表單,它只是向服務器發送很少的請求,並在同一頁面中加載響應。當我檢查你的代碼時,你的意圖是重定向成功/失敗響應的頁面。爲什麼不提交表單並將其重定向到任何你需要的頁面。無論如何,您正在使用服務器端驗證來驗證用戶。希望我給你一些道理..

+0

Hi Vinoth。我只有一個基於REST的服務器(沒有任何類型的網頁),我最終希望能夠訪問這些服務器,並向客戶端顯示數據 - 無論是瀏覽器還是Android設備等。適用?恐怕我的客戶端編程能力還有很多不足之處。 – Ruaghain

+0

正如我觀察你的回答上面,你正在使用Web服務來得到你的迴應。所以你可能無法更改服務器中的任何類型的代碼(希望)。 –