This is a newbie question. If I have a xml file that I am opening using Ajax.Request, then the response is trapped using responseText. Now this response is a set of xml tags, if I need to traverse through it using DOM, I am not able to use the getElementByTagName method? If I write my own ajax function, then when I do t.responseXML.getElementByTagName(''items'') it works but with responseText it doesn''t work. Any help would be appreciated. Thank you, Mandy.
I think the response needs to come back from the server with a content-type of text/xml (rather than text/html) in order to fill in the responseXML for you. You can''t use getElementsByTagName with responseText because it is just text, not a parsed and loaded DOM tree. Hope that helps... rahul -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Maninder, Singh Sent: Tuesday, October 18, 2005 1:00 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails-spinoffs] responseText Vs responseXML This is a newbie question. If I have a xml file that I am opening using Ajax.Request, then the response is trapped using responseText. Now this response is a set of xml tags, if I need to traverse through it using DOM, I am not able to use the getElementByTagName method? If I write my own ajax function, then when I do t.responseXML.getElementByTagName(''items'') it works but with responseText it doesn''t work. Any help would be appreciated. Thank you, Mandy. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thanks Rahul. But, how can I get the server to return content-type of text/xml? I just have a rss file placed on the server that I am trying to call into my page and parse it. ---------------------------------------------------------------------------------------------------------------------------------- I think the response needs to come back from the server with a content-type of text/xml (rather than text/html) in order to fill in the responseXML for you. You can''t use getElementsByTagName with responseText because it is just text, not a parsed and loaded DOM tree. Hope that helps... rahul ----------------------------------------------------------------------------------------------------------------------------------
Well, it sort of depends on your server I think. Many will look at the ".rss" and set some content-type based on that automatically before sending it back to the client (your ajax call). If you have a script returning the rss on the server, then you need to make a call to set the content type yourself. In php, I think there''s a setHeader(...) function. In java, on the HttpServletResponse I think there is a setContentType method. But if the server doesn''t set it right and its not a script returning it then I''m not sure how to do it. Maybe there''s a "parseXml" function built into JavaScript? I''m sure people have written it, if it''s not there. You could find one of those and just call that with the responseText you get back already and then walk the DOM tree with that the parser returns. Beyond that, I don''t know... rahul -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Maninder, Singh Sent: Tuesday, October 18, 2005 1:19 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails-spinoffs] responseText Vs responseXML Thanks Rahul. But, how can I get the server to return content-type of text/xml? I just have a rss file placed on the server that I am trying to call into my page and parse it. ------------------------------------------------------------------------ ---------------------------------------------------------- I think the response needs to come back from the server with a content-type of text/xml (rather than text/html) in order to fill in the responseXML for you. You can''t use getElementsByTagName with responseText because it is just text, not a parsed and loaded DOM tree. Hope that helps... rahul ------------------------------------------------------------------------ ---------------------------------------------------------- _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
If I write my own code to handle xmlhttp request (basic code) I am able to do a responseXML and traverse through the tree using DOM. However, seems like prototype is doing something as responseXML is always null. In the former case, I did not set any special headers. I am just accessing a rss file placed on my servers. Hasn''t anyone faced this before? Thank you, Mandy. -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On Behalf Of Rahul Bhargava Sent: Tuesday, October 18, 2005 11:01 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails-spinoffs] responseText Vs responseXML Well, it sort of depends on your server I think. Many will look at the ".rss" and set some content-type based on that automatically before sending it back to the client (your ajax call). If you have a script returning the rss on the server, then you need to make a call to set the content type yourself. In php, I think there''s a setHeader(...) function. In java, on the HttpServletResponse I think there is a setContentType method. But if the server doesn''t set it right and its not a script returning it then I''m not sure how to do it. Maybe there''s a "parseXml" function built into JavaScript? I''m sure people have written it, if it''s not there. You could find one of those and just call that with the responseText you get back already and then walk the DOM tree with that the parser returns. Beyond that, I don''t know... rahul -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Maninder, Singh Sent: Tuesday, October 18, 2005 1:19 PM To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails-spinoffs] responseText Vs responseXML Thanks Rahul. But, how can I get the server to return content-type of text/xml? I just have a rss file placed on the server that I am trying to call into my page and parse it. ------------------------------------------------------------------------ ---------------------------------------------------------- I think the response needs to come back from the server with a content-type of text/xml (rather than text/html) in order to fill in the responseXML for you. You can''t use getElementsByTagName with responseText because it is just text, not a parsed and loaded DOM tree. Hope that helps... rahul ------------------------------------------------------------------------ ---------------------------------------------------------- _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs