2012-03-20 61 views
0

我试图使用javascript和XMLHttpRequest实现异步登录到JEE6 webapp。我应该能够使用XMLHttpRequest对/ app/j_security_check进行异步调用,并以某种方式解析响应,以便我可以向用户显示“登录失败”或“登录成功”的对话框。我正在使用Glassfish 3.1.1。在Glassfish 3.1.1中的Java EE web应用程序异步登录

我试过的东西,但回应总是为空。我有保存登录表单和下面的脚本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安全性在成功/失败登录后执行。它似乎总是重定向页面,无论我用javascript做什么。我也尝试过使用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中做任何事情。 – 2012-05-02 19:04:49

+0

@Antionio你能更新你的问题吗?你是否添加了if(client.readyState == 4)if(client.status == 200){'in'onreadystatechange'方法,因为我建议? – 2012-05-02 19:14:59

+0

我们实际上放弃了整个想法,没有异步登录。 – 2013-01-16 08:29:25