Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
JavaScript
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:
Java
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... :)
Posted
Updated 10-Feb-11 0:10am
v3
Comments
jswolf19 10-Feb-11 4:56am    
What browser(s) are you testing in?

1 solution

If you're testing in Internet Explorer, then there is a caching problem as described, for example, here[^] and here[^]. This may be what is causing your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900