amlsoft
12-30-2010, 08:28 AM
Hi guys im new to javascript programming... Im developing a JSP site and to validate a JSP page I used this code which was downloaded...
But It its not working properly and giving me a message as responseXml is null...
JS file as...
function validate(inputValue, fieldID){
// only continue if xmlHttp isn't void
if (xmlHttp){
// if we received non-null parameters, we add them to cache in the
// form of the query string to be sent to the server for validation
if (fieldID){
// encode values for safely adding them to an HTTP request query string
inputValue = encodeURIComponent(inputValue);
fieldID = encodeURIComponent(fieldID);
// add the values to the queue
cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
}
// try to connect to the server
try{
// continue only if the XMLHttpRequest object isn't busy
// and the cache is not empty
if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0){
// get a new set of parameters from the cache
var cacheEntry = cache.shift();
// make a server request to validate the extracted data
xmlHttp.open("POST", serverAddress, true);
// xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
// xmlHttp.overrideMimeType("text/xml");
xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlHttp.onreadystatechange = handleRequestStateChange
xmlHttp.send(cacheEntry);
}
}catch (e){
// display an error when failing to connect to the server
displayError(e.toString());
}
}
}
// function that handles the HTTP response
function handleRequestStateChange(){
// when readyState is 4, we read the server response
if (xmlHttp.readyState == 4){
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200){
try{
// read the response from the server
readResponse();
}catch(e){
// display error message
displayError("HERE AT METHOD CALL >"+e.toString());
}
}else{
// display error message
displayError(xmlHttp.statusText);
}
}
}
// read server's response
function readResponse(){
// retrieve the server's response
var response = xmlHttp.responseText;
// server error?
if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
throw(response.length == 0 ? "Server error." : response);
// get response in XML format (assume the response is valid XML)
responseXml = xmlHttp.responseXML;
// get the document element
xmlDoc = responseXml.documentElement;
result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
// find the HTML element that displays the error
message = document.getElementById(fieldID + "Failed");
// show the error or hide the error
message.className = (result == "0") ? "error" : "hidden";
// call validate() again, in case there are values left in the cache
setTimeout("validate();", 250);
}
// sets focus on the first field of the form
function setFocus(){
document.getElementById("txtUsername").focus();
}
JSP file as..
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="validate.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="validate.js"></script>
<script type="text/javascript" src="urchin.js"></script>
<script type="text/javascript">
_uacct = "UA-108340-1";
urchinTracker();
</script>
</head>
<body onload="setFocus()">
<br/><br/>
<fieldset>
<legend class="txtFormLegend">NEW MEMBER REGISTRATION FORM</legend>
<br />
<form name="frmRegistration" method="POST" action="">
<!-- Username -->
<label for="txtUsername">Desired username:</label>
<input id="txtUsername" name="txtUsername" type="text"
onBlur="validate(this.value, this.id)"
value="" />
<span id="txtUsernameFailed"
class="hidden">
This username is in use, or empty username field. </span>
<br />
<!-- Name -->
<label for="txtName">Your name:</label>
<input id="txtName" name="txtName" type="text"
onblur="validate(this.value, this.id)"
value="" />
<span id="txtNameFailed"
class="hidden">
Please enter your name.
</span>
But It its not working properly and giving me a message as responseXml is null...
JS file as...
function validate(inputValue, fieldID){
// only continue if xmlHttp isn't void
if (xmlHttp){
// if we received non-null parameters, we add them to cache in the
// form of the query string to be sent to the server for validation
if (fieldID){
// encode values for safely adding them to an HTTP request query string
inputValue = encodeURIComponent(inputValue);
fieldID = encodeURIComponent(fieldID);
// add the values to the queue
cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
}
// try to connect to the server
try{
// continue only if the XMLHttpRequest object isn't busy
// and the cache is not empty
if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0){
// get a new set of parameters from the cache
var cacheEntry = cache.shift();
// make a server request to validate the extracted data
xmlHttp.open("POST", serverAddress, true);
// xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
// xmlHttp.overrideMimeType("text/xml");
xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlHttp.onreadystatechange = handleRequestStateChange
xmlHttp.send(cacheEntry);
}
}catch (e){
// display an error when failing to connect to the server
displayError(e.toString());
}
}
}
// function that handles the HTTP response
function handleRequestStateChange(){
// when readyState is 4, we read the server response
if (xmlHttp.readyState == 4){
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200){
try{
// read the response from the server
readResponse();
}catch(e){
// display error message
displayError("HERE AT METHOD CALL >"+e.toString());
}
}else{
// display error message
displayError(xmlHttp.statusText);
}
}
}
// read server's response
function readResponse(){
// retrieve the server's response
var response = xmlHttp.responseText;
// server error?
if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
throw(response.length == 0 ? "Server error." : response);
// get response in XML format (assume the response is valid XML)
responseXml = xmlHttp.responseXML;
// get the document element
xmlDoc = responseXml.documentElement;
result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
// find the HTML element that displays the error
message = document.getElementById(fieldID + "Failed");
// show the error or hide the error
message.className = (result == "0") ? "error" : "hidden";
// call validate() again, in case there are values left in the cache
setTimeout("validate();", 250);
}
// sets focus on the first field of the form
function setFocus(){
document.getElementById("txtUsername").focus();
}
JSP file as..
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="validate.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="validate.js"></script>
<script type="text/javascript" src="urchin.js"></script>
<script type="text/javascript">
_uacct = "UA-108340-1";
urchinTracker();
</script>
</head>
<body onload="setFocus()">
<br/><br/>
<fieldset>
<legend class="txtFormLegend">NEW MEMBER REGISTRATION FORM</legend>
<br />
<form name="frmRegistration" method="POST" action="">
<!-- Username -->
<label for="txtUsername">Desired username:</label>
<input id="txtUsername" name="txtUsername" type="text"
onBlur="validate(this.value, this.id)"
value="" />
<span id="txtUsernameFailed"
class="hidden">
This username is in use, or empty username field. </span>
<br />
<!-- Name -->
<label for="txtName">Your name:</label>
<input id="txtName" name="txtName" type="text"
onblur="validate(this.value, this.id)"
value="" />
<span id="txtNameFailed"
class="hidden">
Please enter your name.
</span>