Currently, I am incorporating XmlHttp with Java servlets in the following manner:
function btnSave_onclick(){
var xmlHttp;
var responseText;
if (condition){
var para= "someParamsHere";
var url = "urlHere";
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('post', url, true);
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
xmlHttp.setRequestHeader("Content-length", para.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(para);
xmlHttp.onreadystatechange=function() {
if (xmlHttp.readyState==4) {
if(xmlHttp.status==200){
responseText=xmlHttp.responseText;
if(responseText=='error'){
alert("Fatal Error Occurred");
return;
}
}
}
}
}
// additional code goes here
}
The information is sent to the servlet and if an exception arises, the response text containing the word 'error' is displayed. The issue is that the error alert pops up but the code proceeds to the section // additional code goes here without exiting the JavaScript function on return. Any suggestions on this matter would be much appreciated.