Forum: Ruby on Rails Parsing to_xml respond_to with Javascript

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David H. (Guest)
on 2007-06-21 22:42
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.

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 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
(Guest)
on 2007-06-22 18:29
(Received via mailing list)
Have you considered using to_json?

On Jun 21, 1:42 pm, David H. <removed_email_address@domain.invalid>
divotdave (Guest)
on 2007-06-22 19:57
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), 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 for the benefit of others. Anyone who has already
done this is welcomed to do the same.

Thanks,
divotdave
This topic is locked and can not be replied to.