# Helpers in rails to get URL using AJAX

**URL:** <https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040>\
**Category:** Rails\
**Created:** [August 27, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040 "2008-08-27T22:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thib21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Thib21](https://www.ruby-forum.com/u/Thib21)\
**Post date:** [August 27, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040/1 "2008-08-27T22:19:49Z")

</div>

Hello all,  
I’m trying to get (using Ajax) whatever URL the user clicks on the  
page. Also, I want to store the results from the URL request in a JS  
variable.  
Did somebody know helper in Rails which allow me to do that ?  
Thanks,  
Thib

---

<div class="post-metadata">

**Author:** ![Thib21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Thib21](https://www.ruby-forum.com/u/Thib21)\
**Post date:** [August 28, 2008, 4:05am UTC](https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040/2 "2008-08-28T04:05:06Z")

</div>

Well, I have found some interesting code :  
([Prototype JavaScript Framework | Introduction to Ajax](http://prototypejs.org/learn/introduction-to-ajax))

# 

new Ajax.Request(‘/some\_url’,  
{  
method:‘get’,  
onSuccess: function(transport){  
var response = transport.responseText || “no response text”;  
alert(“Success! \n\n” + response);  
},  
onFailure: function(){ alert(‘Something went wrong…’) }  
});

## 

But, instead of using ‘/some\_url’, I would like to use 'http://  
a\_different\_domain.com…"

According to the “Prototype” library website :  
“Ajax requests can only be made to URLs of the same protocol, host and  
port of the page containing the Ajax request”  
 → does somebody know if it’s only a “Prototype” library restriction,  
or if it’s a restriction for Ajax in general ? if it’s not, somebody  
have an idea to make it work ?

Thanks  
Thib

---

<div class="post-metadata">

**Author:** ![Thib21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Thib21](https://www.ruby-forum.com/u/Thib21)\
**Post date:** [August 29, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040/3 "2008-08-29T12:57:58Z")

</div>

Not sure, but I think it’s a general restriction of XMLHTTPRequest in  
browsers in order to avoid security loopholes. It would be too easy if  
this were possible to have a user submit information to other websites  
without them knowing.

Dirk.

---

<div class="post-metadata">

**Author:** ![Thib21](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Thib21](https://www.ruby-forum.com/u/Thib21)\
**Post date:** [August 29, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/helpers-in-rails-to-get-url-using-ajax/147040/4 "2008-08-29T13:54:53Z")

</div>

It is indeed a javascript security restriction for the reason  
mentioned below. The best solution is to submit the request to your  
server and have the server fetch the page and process it (send it back  
as a response). And usually it’s a good idea to cache those offsite  
fetched responses.

On 29 Aug 2008, at 12:57, deegee wrote:

> > ## 
> > 
> > or if it’s a restriction for Ajax in general ? if it’s not, somebody  
> > have an idea to make it work ?

Best regards

Peter De Berdt
