Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in implementing the AJAX with Struts

 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

i have the issue AJAX with Struts.

i am providing the code here.

in JSP JavaScript

request.onreadystatechange = replyMethod(request);
request.open('GET',url,true);
request.send(null);

function replyMethod()
{
var message = request.responseXML.getElementsByTagName("message")[0];
}

here i am getting problem , i am unable to get the responseXML object.



in Action Class perform()

response.setContentType("text/xml");
response.setHeader("Cache-Control","no-cache");
response.getWriter().write("some value");

return mapping.findForward("Success");



my issue is i am able to get the request.responseText
but i am unable to get the request.responseXML

how to return the xml from Action class


please help me in this way
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not an advanced Java question; it might be a Javascript question, or a Struts question, but it's not a Java question at all. I suspect it's an issue with understanding Struts, so I'm moving this to our Struts forum. Please stop posting these questions in JiG (Advanced) -- it's not where they belong!
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried returning valid XML from your action? Since, "some value" is not a valid xml document, my understanding is that responseXML will be null.

- Brent
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Brent,


If i provide the code as below in Action class

response.getWriter().write("<message><item-name>Box</item-name></message>");

Is it going to read properly in JavaScript

var message = request.responseXML.getElementByTagName("message")[0];



And tell me whether the Action class is going to validate the XML or JavaScript is going to validate the XML. Where it compare the XML with DTD or Schema.


please provide proper answer , if possible provide some sample code with Action class.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are ahead of me when it comes to Ajax, because I have not actually written any code. My understanding is that responseXML uses an XML parser to build a DOM object representing the XML document. If it cannot parse the XML document, then it...well, I just ran a quick search and got this:

"if the response entity body is not valid XML, this method returns the Document object that was parsed so that you can access the ParseError object through it."

This seems odd. Have you tried using responseText to see what is being returned? Now that I look at your Action code, I would return null instead of mapping.findForward().

- Brent
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Brent,

when i return mapping.findforward("jsp");

if i call request.responseText() it is giving Jsp's html content as output.

but not what i have returned response.getWriter().write("<h>hello</h>");
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i found the one solution that if i return Null in mapping.findFowrad(null);

then if u read the request.responseText then it will give the XML content which we have written in Action class.


but still i am not getting why request.responseXML giving NULL. value
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You indicated tha the code in you JSP was:

request.onreadystatechange = replyMethod(request);
request.open('GET',url,true);
request.send(null);

function replyMethod()
{
var message = request.responseXML.getElementsByTagName("message")[0];
}


Make request a global variable and change:

request.onreadystatechange = replyMethod(request);

to

request.onreadystatechange = replyMethod;
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merrill ,

Yes i am using the Global request variable only.

Although it is giving Null when i print request.responseXML.

If u have any code that is working in your plate kindely u can send it to me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic