PDA

View Full Version : responseXML is null....



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>

amlsoft
12-31-2010, 09:07 AM
Hi guys...
I download this code for form validation... but every time my responseXML object getting null....

Im not loading any external XML file here to load data here.... but still its not working for me....

var serverAddress = "reg.jsp";

function validate(inputValue, fieldID){

if (xmlHttp){

if (fieldID){
inputValue = encodeURIComponent(inputValue);
fieldID = encodeURIComponent(fieldID);
cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
}

try{

if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0){

var cacheEntry = cache.shift();

xmlHttp.open("POST", serverAddress, true);
xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlHttp.onreadystatechange = handleRequestStateChange
xmlHttp.send(cacheEntry);
}
}catch (e){
displayError(e.toString());
}
}
}

function handleRequestStateChange(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
try{
readResponse();
}catch(e){
displayError("HERE AT METHOD CALL >"+e.toString());
}
}else{
displayError(xmlHttp.statusText);
}
}
}

function readResponse(){

var response = xmlHttp.responseText;

if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
throw(response.length == 0 ? "Server error." : response);

responseXml = xmlHttp.responseXML;

xmlDoc = responseXml.documentElement;
result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;

message = document.getElementById(fieldID + "Failed");
message.className = (result == "0") ? "error" : "hidden";

setTimeout("validate();", 250);
}

function setFocus(){
document.getElementById("txtUsername").focus();
}

Thanks...

jscheuer1
12-31-2010, 01:31 PM
Please enclose code in:



code goes here


tags so it will look like:



code goes here



I've looked over the code in both your posts, which I've merged into one thread as they seem to be the same question more or less. There is no xmlHttp defined. So you should be getting an error like:


xmlHttp is undefined

The fact that you are not makes me think that possibly it is defined somewhere else that you're not showing us yet. But there's no way of knowing for sure from what you've shown us.

If you're not making and/or using an "external XML file" here, then you probably should be using completely different code to validate your form.

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.