AJAX, reponseXML undefined on callback

Using AJAX, I am making a POST request to the server to update a couple of variables. The servlet contains the following code:

getHttpResponse().setContentType("text/xml");

getHttpResponse().setHeader("Cache-Control","no-cache");

getHttpResponse().getWriter().println(getXML(var1, var2));

getHttpResponse().getWriter().flush();

The XML created looks similar to this:

<?xml version=\"1.0\"?><var1>10</var1><var2>3</var2>

The callback function is successfully entered, but responseXML remains undefined:

function callback(){

if (req.readyState == 4){

if (req.status == 200){

// printing req.responseXML.value shows 'Undefined'

}

}

}

What am I potentially missing or overlooking? Thanks.

[1145 byte] By [mtong@topcoder.coma] at [2007-10-2 10:43:00]
# 1
Hi, did you find the problem?, I'm having the same, I'm using Ajax with prototype.
jmelgarea at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I had trouble when using Prototype with openrico. If your using this the following method then for some reason the same response returns 1 node in IE and 3 in Firefox.

ajaxResponse.childNodes[0]

Thus, as a workround I used:

ajaxUpdate: function(ajaxResponse)

{

/* IE RETURNS 1 FOR LENGTH AND EXPECTS NODE [0] FIREFOX RETURNS 3 AND EXPECTS NODE [1] */

if(ajaxResponse.childNodes.length == 1)

{

this.setProduct(ajaxResponse.childNodes[0]);

}

else

{

this.setProduct(ajaxResponse.childNodes[1]);

}

}

alan_ha at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I hope the XML data is not exactly like what you illustrate because it's not valid, XML documents must have a single root and you have two.If your XML is valid, what doeshttphandler.responseXML.documentElementcontain?
gimbal2a at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

//AjaxServer.java

import java.io.IOException;

import java.util.HashMap;

import javax.servlet.ServletConfig;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @version 1.0

* @author

*/

public class AjaxServer extends HttpServlet {

private ServletContext context;

private HashMap users = new HashMap();

public void init(ServletConfig config) throws ServletException {

this.context = config.getServletContext();

users.put("greg","account data");

users.put("duke","account data");

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String targetId = request.getParameter("id");

System.out.println("Got Request : "+targetId);

if ((targetId != null) && users.containsKey(targetId.trim())) {

response.setContentType("text/xml");

response.setHeader("Cache-Control", "no-cache");

response.getWriter().write("<message>valid</message>");

} else {

response.setContentType("text/xml");

response.setHeader("Cache-Control", "no-cache");

response.getWriter().write("<message>invalid</message>");

}

}

}

//AjaxClient.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<META name="GENERATOR" content="IBM WebSphere Studio">

<META http-equiv="Content-Style-Type" content="text/css">

<LINK href="theme/Master.css" rel="stylesheet"

type="text/css">

<TITLE>AjaxClient.html</TITLE>

<script language="javascript">

var req;

function validate() {

alert("In Validate");

var idField = document.getElementById("userid");

var url = "<%=request.getContextPath()%>/servlet/AjaxServer?id=" + escape(idField.value);

alert("URL : "+url);

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

} else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");

}

req.open("GET", url, true);

req.onreadystatechange = callback;

req.send(null);

alert("Out Validate");

}

function callback() {

if (req.readyState == 4) {

if (req.status == 200) {

var message = req.responseXML.getElementsByTagName("message")[0];

setMessage(message.childNodes[0].nodeValue);

}

}

}

function setMessage(message) {

mdiv = document.getElementById("userIdMessage");

if (message == "invalid") {

mdiv.innerHTML = "<div style=\"color:red\">Invalid User Id<>";

} else {

mdiv.innerHTML = "<div style=\"color:green\">Valid User Id<>";

}

}

</script>

</HEAD>

<BODY>

<FORM>

<table border=0>

<tr>

<td><input type="text" size="20" id="userid" name="id"></td>

<td><input type="button" value="check" id="userButton" onclick="validate();"></td>

<td><div id="userIdMessage"></div></td>

</tr>

</table>

</FORM>

</BODY>

</HTML>

krasr1a at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
hijust refer following url http://javasoft.4000webs.com/tutorials/ajax.htm
Arunssofta at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I've had this problem before with req being undefined. It turned out that an exception was being thrown in my function that was creating req and by the time it go to the point where you are at it was null. So I guess make sure that there are no errors in the code prior to this
wpr101a at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi,

Why don't U try wid this...

change

var url = "<%=request.getContextPath()%>/servlet/AjaxServer?id=" + escape(idField.value);

to this...

var url = "<SERVELET_PATH_CONFIGURED_@_WEB.XML>?id=" + escape(idField.value);

so change the validate function in your script to something similar to the one below...(here i've made an assuption that the Servlet is being registered under the same web application)

var req

function validate() {

alert("In Validate") ;

var idField = document.getElementById("userid");

var url = /servlet/AjaxServer?id=" + escape(idField.value);

alert("URL : "+url);

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

} else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");

}

if(req == null){

alert("Browser cannot support XmlHttpRequest");

return;

} else{

req.onreadystatechange = callback;

req.open("GET", url, true);

req.send(null);

}

}

Hope this might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-13 2:52:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...