File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes AJAX ResponseXML not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX ResponseXML not working" Watch "AJAX ResponseXML not working" New topic
Author

AJAX ResponseXML not working

Steve Renard
Ranch Hand

Joined: Apr 27, 2007
Posts: 66
I'm trying to send an AJAX request to query table data and print response..
But the ResponseXML is showing empty..
However, ResponseText works well.
Here's my javascript function involving AJAX and down below is my server side code snippet.

function initRequest()
{
var msg = document.getElementById('msg');
msg.childNodes[0].nodeValue = "Sending request..";
document.frmData.btnSend.disabled = true;

var url = "servlet/UNExt?par=" + new Date().getTime();
var queryString = "&Type=fetch&quart=4"+
createXMLHttpRequest();
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
xmlHttp.send(queryString);
}

function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var msg = document.getElementById('msg');
var result = xmlHttp.responseXML.getElementsByTagName("RESPONSE")[0].firstChild.data;
if (result == "OK")
msg.firstChild.nodeValue = 'Success';
else msg.firstChild.nodeValue = 'Error';
}
}
}


SERVER SIDE CODE:
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");

out.println("<RESPONSE>" + res+ "</RESPONSE>");
out.close();

Please help..
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Everything looks ok to me. But what you didn't show is the XML you are sending back to the browser. I'd guess that there is something wrong with your XML and that is why you aren't getting it back. At least, that's usually what happens to me. XML probably isn't well formed. Missing closing element or missing parent elements, etc. Things to look for.


GenRocket - Experts at Building Test Data
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Follow this:
http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html
and you should get your problem fixed.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

Please be sure to use UBB code tags when posting code to the forums. Please read this for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Renard
Ranch Hand

Joined: Apr 27, 2007
Posts: 66
I'm using the server side code in Struts Action..
I have this at the end of the code:
return actionMapping.findForward("success");
It rebuilds the matching JSP file.
Does that overwrite my Printwriter contents?.
Steve Renard
Ranch Hand

Joined: Apr 27, 2007
Posts: 66
Please help me resolve this..
My SERVER SIDE CODE:

I still see the responseXML as null.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You do not have valid XML which is one of the steps in that link.

This link shows you the basic xml syntax, hopefully you see what is missing. http://www.w3schools.com/xml/xml_syntax.asp

Eric
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Eric Pascarello:
You do not have valid XML which is one of the steps in that link.

This link shows you the basic xml syntax, hopefully you see what is missing. http://www.w3schools.com/xml/xml_syntax.asp

Eric


Do we really no that? He hasn't shown his XML to us yet. But that would be the best bet.
 
 
subject: AJAX ResponseXML not working