Hello Dear Friends,
I am using following Ajax Call to check whether particular employee is available in database or not:
<input type="text" id="txtEmpCode" name="txtEmpCode" onblur="isEmpCodeExists(this);" />
And my Javascript functions for Ajax call are as follows:
function isEmpCodeExists(obj)
{
var val = obj.value;
if(val != "") {
hdnObj.value = val;
var url="MyServlet?value=" + encodeURIComponent(val);
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.onreadystatechange = callbackIsTransCodeExists;
req.send(null);
}
}
function callbackIsTransCodeExists() {
var ind_ele = document.getElementById(ind);
ind_ele.style.display = "none";
if (req.readyState == 4) {
if (req.status == 200) {
parseMessageIsTransCodeExists();
}
} else {
ind_ele.style.display = "";
}
}
function parseMessageIsTransCodeExists() {
var message = req.responseXML.getElementsByTagName("message")[0];
var m = message.childNodes[0].nodeValue;
}
Now Following my Servlet doGet() method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String transCode = request.getParameter("value");
.......
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<message>true</message>");
}
It works fine with one value 'true' or 'false'.
Now if I uses
response.getWriter().write("<empcode>true</empcode>");
in servlet and
var message = req.responseXML.getElementsByTagName("empcode")[0];
in javascript my "message" will get null values.
It also happens when I pass multiple values as-
response.getWriter().write("<message><val1>val1</val1><val2>val2</val2></message>");
from servlet.
Can you please give me the solution for it as I want to pass multiple records from database using Ajax call?
Please kindly reply... :)