# Parsing to\_xml respond\_to with Javascript

**URL:** <https://www.ruby-forum.com/t/parsing-to-xml-respond-to-with-javascript/107437>\
**Category:** Rails\
**Created:** [June 21, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/parsing-to-xml-respond-to-with-javascript/107437 "2007-06-21T20:42:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [June 21, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/parsing-to-xml-respond-to-with-javascript/107437/1 "2007-06-21T20:42:00Z")

</div>

Hey Everyone:

I am working on a site that is NOT a Rails app, but needs to access data  
from one that is. I would like to do some AJAXy stuff and have the data  
show up in a div based on a user request without redirecting to the  
Rails App Site or refreshing the current page. I believe retrieving the  
data from the Rails App in XML format is my best bet.

my controller action looks like this:

def index  
@foo = Bar.find(:all)

```
respond_to do |format|
  format.html
  format.xml { render :xml => @foo.to_xml }
end

```

end

So my questions is - how can I parse this data using javascript within  
the non-Rails site? I have been using some examples from  
[http://www.w3schools.com/ajax/ajax\_responsexml.asp](http://www.w3schools.com/ajax/ajax_responsexml.asp).

I know this is possible, but when I try it nothing happens - the xml  
never seems to load (it stalls at xmlhttprequest.open(“GET”,url,true) -  
where url is [http://myniftyrails](http://myniftyrails) [app.com/foo.xml](http://app.com/foo.xml)

If I type this url into my browser the result is an xml document tree as  
expected. When I try it via the AJAX call I get nothing.

I assume this would be something like how you would handle data received  
via an rss feed from Rails or a data-grid type thing (neither of which  
I’ve done). This is my first attempt at creating a web-service feature  
in Rails, so please be kind 😉

Thanks,  
divotdave

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [June 22, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/parsing-to-xml-respond-to-with-javascript/107437/2 "2007-06-22T16:29:44Z")

</div>

Have you considered using to\_json?

On Jun 21, 1:42 pm, David H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [June 22, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/parsing-to-xml-respond-to-with-javascript/107437/3 "2007-06-22T17:57:08Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Have you considered using to\_json?

Yes I have thought of to\_json and thanks for the suggestion.

Since making this post I have discovered that (of all things) Mozilla,  
Safari and Opera (not IE) have a security restriction built in that does  
not allow cross-domain xmlhttprequests (AJAX). Essentially the request  
must be within the same domain the browser is currently accessing. This  
is by design as it can open up some security holes if not specifically  
guarded (though this kind of behavior is evident in Google’s Maps API  
for example), but is also a road-block for AJAX drive cross-domain web  
services.

I found an example of a php script to bi-pass this issue on the Yahoo  
Developer’s Network  
([http://developer.yahoo.net/javascript/howto-proxy.html](http://developer.yahoo.net/javascript/howto-proxy.html)), which puts a  
web proxy page on the server that is called by the client, accesses the  
data on the server and sends it back to the client to be parsed by  
javascript and inserted into the page as expected - thereby fooling the  
browser into thinking that the request is going within the same domain.

I am going to work on a Ruby port of this script and will post it here  
and on [railsforum.com](http://railsforum.com) for the benefit of others. Anyone who has already  
done this is welcomed to do the same.

Thanks,  
divotdave
