Jump to content

Ajax XML Response - Undefined


viswa46

Recommended Posts

post-112405-0-42026800-1381293729_thumb.pngpost-112405-0-42026800-1381293729_thumb.pngHi All,

 

i'm doing sample ajax exercise and getting the response from server but the response work fine in IE browser but not in firefox/chrome browser.

 

below is the code snippet also find the snippet

 

=========================

ajaxdemo.html

=========================l

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" id="inputDisplay" />
<button type="button" onclick="sendMessageToServer()">Send To Server</button><br/>
<input type="text" id="textDisplay"/>
<script type="text/javascript">
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function sendMessageToServer() {
xmlhttp.open("POST","/LabAjax04/AjaxDemoServlet?name="+document.getElementById("inputDisplay").value,true);
xmlhttp.onreadystatechange=receiveMessageFromServer;
xmlhttp.send();
document.getElementById("inputDisplay").value='';
}
function receiveMessageFromServer() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text);// displays undefined
alert(xmlhttp.responseText);//display the response from server
document.getElementById("textDisplay").value=xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text;
}
}
</script>
</body>
</html>

 

=======================

java file

======================

package com.sample.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AjaxDemoServlet
*/
public class AjaxDemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AjaxDemoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("server recived the folwwoing messgae from client:"+request.getParameter("name"));
response.setContentType("text/xml");
response.getWriter().println("<responseFromServer>"+request.getParameter("name")+"</responseFromServer>");
System.out.println("server replied with the folwwoing messgae to client:"+request.getParameter("name"));
}
}
=========
web.xml
=========
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LabAjax04</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>AjaxDemoServlet</display-name>
<servlet-name>AjaxDemoServlet</servlet-name>
<servlet-class>com.sample.ajax.AjaxDemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxDemoServlet</servlet-name>
<url-pattern>/AjaxDemoServlet</url-pattern>
</servlet-mapping>
</web-app>
Thanks in advance!!!

 

Link to comment
Share on other sites

Hi EveryOne,
In the below snippet is there any syntaxt error or is there a better way to parse the xml doc.
alert(xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text);// displays undefined
alert(xmlhttp.responseText);//display the response from server
document.getElementById("textDisplay").value=xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text;
Regards,
Viswa
Link to comment
Share on other sites

I don't see a node in your XML response called responseFromServer

Edited by thescientist
Link to comment
Share on other sites

Hi ,

Please find the below snippet

 

<script type="text/javascript">
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function sendMessageToServer() {
xmlhttp.open("POST","/LabAjax04/AjaxDemoServlet?name="+document.getElementById("inputDisplay").value,true);
xmlhttp.onreadystatechange=receiveMessageFromServer;
xmlhttp.send();
document.getElementById("inputDisplay").value='';
}
function receiveMessageFromServer() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text);// displays undefined
alert(xmlhttp.responseText);//display the response from server
document.getElementById("textDisplay").value=xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text;
}
}

 

Thanks,

Viswa

Link to comment
Share on other sites

Look at your code

 

 

function receiveMessageFromServer() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text);// displays undefined
alert(xmlhttp.responseText);//display the response from server
document.getElementById("textDisplay").value=xmlhttp.responseXML.getElementsByTagName("responseFromServer")[0].text;
}
}

You're looking to get an element with a tag name of responseFromServer. that's I am trying to point out to you.

Link to comment
Share on other sites

You original post contains code from what you called web.xml. I don't see how the rest of your code is using web.xml or why you posted that. It looks like the Java code is sending the response here:

 

response.setContentType("text/xml");response.getWriter().println("<responseFromServer>"+request.getParameter("name")+"</responseFromServer>");

 

I would start by printing the entire responseXML to see what is there, or using console.log to look at it. It looks like it is showing up in the XML response tab in Firebug, so you need to find out what is in responseXML.

  • Like 1
Link to comment
Share on other sites

Hi,

 

Thanks for the suggestion the console.log done the trick for me it helped to debug the responseXML.

 

document.getElementById("textDisplay").value=xmlhttp.responseXML.docElement.childNodes[0].text // It works
Thanks again!!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...