2012-03-20 4 views
0

javascript 및 XMLHttpRequest를 사용하여 JEE6 webapp에 비동기 로그인을 구현하려고합니다./app/j_security_check에 XMLHttpRequest를 사용하여 비동기 호출을하고 응답을 구문 분석하여 "Login Failed"또는 "Login success"라는 대화 상자를 사용자에게 표시 할 수 있어야합니다. Glassfish 3.1.1을 사용하고 있습니다.Glassfish의 Java EE 웹 응용 프로그램 비동기 로그인 3.1.1

내가 시도했지만 반응은 항상 null입니다. 로그인 폼과 다음 스크립트를 보유하고있는 login.jsp가 있습니다 :

function submitLogin(formName) { 
    var urlAction = "/app/j_security_check"; 
    var client; 
    var dataString; 
    if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 
     client = new XMLHttpRequest(); 
    } else { // IE6, IE5 
     client = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    client.onreadystatechange = function() { 
     var response = client.responseText; // this is always null 

        /* ALERT THE DIALOG HERE ACCORDING TO RESPONSE? */ 
    }; 

    var form = document.forms[formName]; 
    var username = form.elements["j_username"].value; 
    var password = form.elements["j_password"].value; 
    dataString = "j_username=" + username + "&j_password=" + password; 
    client.open("POST", urlAction, true); 
    client.setRequestHeader("Content-type", 
      "application/x-www-form-urlencoded"); 
    client.send(dataString); 
} 

제 질문은 이것이 가능한지 어떻게 구현해야합니까?

편집 : 여기에서 발생하는 문제는 리디렉션 Java Security가 성공적/실패한 로그인 후에 적용되는 것으로 보입니다. 그것은 자바 스크립트와 상관없이 항상 페이지를 리디렉션하는 것으로 보입니다. jQuery의 ajax 메소드도 사용하지 않았다. 나는 비슷한 할

답변

0

어쩌면 이것은 당신을 도울 것입니다 :

 //Get a XMLHttpRequest object. 
     //The XMLHttpRequest specification defines an API that provides 
     //scripted client functionality for transferring data between a client and a server. 
     function getXMLObject() //XML OBJECT 
     { 
      var xmlHttp = false; 
      try { 
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers 
      } 
      catch (e) { 
      try { 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+ 
      } 
      catch (e2) { 
       xmlHttp = false // No Browser accepts the XMLHTTP Object then false 
      } 
      } 
      if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { 
      xmlHttp = new XMLHttpRequest();  //For Mozilla, Opera Browsers 
      } 
      return xmlHttp; // Mandatory Statement returning the ajax object created 
     } 

     var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax obj 

     /* 
     * Use this method to send data to server using ajax. 
     * Sent attribute name is : attribute 
     * Sent attribute value is : attribute:val 
     */ 
     function ajaxFunction(attribute,val, url) { 
      if(xmlhttp) { 
      var param = attribute+"="+attribute+":"+val; 
      param +="&tiers_payant="+document.getElementsByName("tiers_payant")[0].value; //Add the value to send here 
      xmlhttp.open("POST",url,true); 
      xmlhttp.onreadystatechange = handleServerResponse; 
      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
      xmlhttp.send(param); 
      } 
     } 

     /** 
     * When client received response from server, 
     * set the inner HTML of the page with the one returned by server. 
     */ 
     function handleServerResponse() { 
      if (xmlhttp.readyState == 4) { 
      if(xmlhttp.status == 200) { 
       // DO what you want with : xmlhttp.responseText; 

      } 
      else { 
       document.getElementById("erreur").innerHTML="Le serveur le répond pas."; 
       //alert("Error during AJAX call. Please try again"+xmlhttp.status); 
      } 
      } 
     } 
+0

이 작동하지 않습니다. 페이지는 로그인 후 항상 리디렉션되므로 기본 Javascript 또는 jQuery에서 아무 것도 처리하지 못하는 것 같습니다. –

+0

@Antionio 질문을 귀하가 시도한 것으로 업데이트 할 수 있습니까? 내가 제안한대로'if (client.readyState == 4) { if (client.status == 200) {'onreadystatechange' 메소드에 추가 했습니까? –

+0

우리는 실제로 전체 아이디어를 폐기하고 비동기 로그인없이 진행했습니다. –

관련 문제