Glassfish 3.1.1中的Java EE web应用程序异步登录
我正在尝试使用javascript和XMLHttpRequest实现对JEE6WebApp的异步登录。我应该能够使用XMLHttpRequest对/app/j_security_check进行异步调用,并以某种方式解析响应,以便向用户显示一个带有“登录失败”或“登录成功”的对话框。我使用的是Glassfish 3.1.1 我尝试了一些东西,但响应总是空的。我有一个login.jsp,其中包含登录表单和以下脚本:Glassfish 3.1.1中的Java EE web应用程序异步登录,java,javascript,asynchronous,java-ee-6,glassfish-3,Java,Javascript,Asynchronous,Java Ee 6,Glassfish 3,我正在尝试使用javascript和XMLHttpRequest实现对JEE6WebApp的异步登录。我应该能够使用XMLHttpRequest对/app/j_security_check进行异步调用,并以某种方式解析响应,以便向用户显示一个带有“登录失败”或“登录成功”的对话框。我使用的是Glassfish 3.1.1 我尝试了一些东西,但响应总是空的。我有一个login.jsp,其中包含登录表单和以下脚本: function submitLogin(formName) { var u
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方法,但没有成功。我做了类似的事情,也许这会对您有所帮助:
//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);
}
}
}
这似乎不起作用。页面总是在登录后重定向,我似乎无法在本机Javascript或jQuery中对此做任何操作。@Antionio您能用您尝试过的内容更新您的问题吗?您是否按照我的建议在
onreadystatechange
方法中添加了if(client.readyState==4){if(client.status==200){
。