Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AJAX query

 
fahad siddiqui
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using ajax with struts and i open a .do action. In the action i need to write some data on the response and let it go for the callback method of ajax to catch it.

First, responseXML is giving blank on the call back method in AJAX.
Second, responseText is not giving the value i am writing on the response from server side. I think there is something wrong in writing from the server.

I have written the below code:
response.setContentType("text/xml");

PrintWriter out=response.getWriter();
out.print("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>");
out.println("<message>");
out.println("fahad");
out.println("</message>");
retString="success";

I return success from my action which has the forward path to the same jsp from which request came.

This is my javascript side code :

function parseDataRetrieved(){
alert("this is data "+ aReq.responseXML);
response = aReq.responseText;

responseText gives values but the not the ones i am writing on server side.
responseXML is blank.

Please advise with the sample code .
 
fahad siddiqui
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I request for XML from the server using AJAX.
After getting the response, I want to close the connection because, then on another click i want to send another request to a different URL.

But, right now the request is not going so i am thinking, this is because the previous connection did not close and hence, a new request is not getting sent.
What might be the reason and solution?
I am using struts and calling an action of struts from AJAX.

Please advise.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is either a Struts question (for which we have a forum) or a Javascript question (for which we have a forum) but it's not an "Other Java APIs" question, since I don't see where there are any Java APIs involved here.

I suspect this is a problem with abusing Struts, so that's the forum I will move this to.
 
Vani Bandargal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to return null from your action class method.
I made some corrections.
If you are adding more <messages> elements, make sure that you have a root element(XML standard)

try this.


response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out=response.getWriter();
out.flush();
out.print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
out.println("<message>");
out.println("fahad");
out.println("</message>");
return null;

function parseDataRetrieved(){
alert(aReq.responseXML);
alert(aReq.responseText);

[ October 03, 2006: Message edited by: Vani D Bandargal ]
[ October 03, 2006: Message edited by: Vani D Bandargal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic