I am using AJAX in my web application to verify if a document with a particular value already exists in the database. If true, I would like to prompt the user that a document with that title already exists and discontinue with the submission of the document. I am using the code that is available in Andrei Kouvchinnikov’s web site. I have modified the agent to perform the necessary function and the AJAX call is working properly to return or , too. When I use the alert statement to display the responseText, it displays the correct value to me, as well. However, my JavaScript if statement to check if the result == ‘’ is always returning as false. Hence, the document is submitted even though a document with similar title already exists in the database. I am certain that the problem lies with the way I am handling the responseText. The content type of the output of the agent is text/xml. The corresponding JavaScript code to handle the response text is below. Could somebody please point out if I am doing this incorrectly?
function GetResponse(){
…
…
result = http_request.responseText;
if (result == ‘’) {
alert(…);
returnFlag = ‘Found’;
}
}
In the above code, if I embedd a line to write the responseText to a field, it shows or depending on the result of the operation. However, the JavaScript if statement always fails. Do I need to handle the responseText in a special manner? Does the responseText need to be massaged in a certain way before it can be used in an if statement? Thanks for any insights provided.
This is a typical syntax for an Ajax callback. This is live code, with the only changes made being in that typically, I use the Prototype library, which is fairly lightweight, and refer to elements on the form with the syntax $(‘element1’).style.display = ‘none’;
For more complex returns than a single node, using the responseXML is faster and generally easier than the responseText in most cases, though I have returned HTML from a backend agent or Java servlet and passed that html directly into a container div on occasion.
if (req.readyState == 4){
if (req.status == 200){
//window.alert("response text is \n" + req.responseText);
var msg = req.responseXML;
var xmlDocument = msg.documentElement;
if(!xmlDocument){
//throw up an error and return false
window.alert("could not get the document. An error occurred trying to verify deletion of this document.\nIf the evaluation is still in your list, please contact support.");
return false;
}
xmlDocument.normalize();
var message = xmlDocument.getElementsByTagName("data")[0].childNodes[0].nodeValue;
if (Left(message, 5) == "error"){ //Some of the backend error messages start with 'error' and can be displayed here.
window.alert(message + "\nIf the evaluation is still in your list, please contact support.");
return false;
} //end if
if (Left(message, 7) == "failure"){ //the remainder of the failure messages start with 'failure' and are displayed here.
window.alert(message + "\nIf the evaluation is still in your list, please contact support.");
return false;
}
if (Left(message, 7) == "Success"){
//need to be able to clear the cache here.
//if deleting show the donotdelete span
var delmsg = '';
if (Right(message, 4) == "True"){
document.getElementById('donotdelete').style.display = "inline";
document.getElementById('deleteme').style.display = "none";
document.getElementById('delmessage').innerHTML = "This document has been marked for deletion.";
document.getElementById('delmessage').style.display = "inline";
}else{
//if undeleting show the deleteme span
document.getElementById('deleteme').style.display = "inline";
document.getElementById('donotdelete').style.display = "none";
document.getElementById('delmessage').innerHTML = "This document has been unmarked and will not be deleted.";
document.getElementById('delmessage').style.display = "inline";
}
}else{
//a failure occurred, add an error message
window.alert("An error occurred marking the document for deletion or undeletion.\nPlease notify support to delete or undelete this document.");
return false;
} //end if
//remove the objects
xmlDocument = null;
msg = null;
return true;
}else{
window.alert("Requst status:\n" + req.status + "\n " + req.responseText + "\n" + "URL PASSED IN:\n" + url);
return false;
} //end if
} //end if
the returned xml looks like this:
, or like this error xxx on line 100failure deleting the document. This document has been locked and cannot be deleted. Please contact your disctrict manager to delete the document.