如何在 Ruby on Rails 3.0 中对不同域进行 AJAX 调用

发布于 2024-11-29 09:52:11 字数 1490 浏览 1 评论 0原文

我在 www.example.com 上运行的应用程序控制器中有一个操作 email ,我正在尝试将 email 的表单数据发送到 www.data。 example.com/email 我的另一个应用程序在其中接收请求,并且我能够以 js 格式保存数据。但我想将确认发送回 www.example.com 并使用 rjs 模板替换 html。以下是一些代码供您参考:

email.html.erb 调用 www.example.com

   <div id="div_content">
    <%= form_for(@user, :url => "http://data.example.com/mail", :remote => true) do |f| %>
    <%= f.label :email %>
    <%= f.text_field :email%>
    <% end %>
   </div>

电子邮件应用程序操作:data.example.com/email -

def email
  @user = User.create(params[:user])
  respond_to do |format|
    if @user.save!
       format.html { redirect_to(user_page_path(@user.vip_id), :notice => 'Thank you! You are now on our priority list.') }
       format.js
    else
       format.html { render :text => "user can not be saved at this moment!"}
    end
  end
end

email.js.rjs 调用 www.data.example.com/email

page.replace_html :div_content, :partial => "show", :object => @user

我可以在日志中看到请求从一个域一路传到子域,甚至操作被触发,但是我无法将响应返回到主域。那么,有什么方法可以向主域发送回调。我只想反映 div_content div 内表单的更改,并希望替换为我的子域中的 _show.html.erb 内容。

非常感谢,
苏里亚:)

I have an action email in my controller of application running on www.example.com and I am trying to send the form data of email to www.data.example.com/email where my another application receives the request and I am able to save the data in js format. But I want to send back the acknowledgement to www.example.com and replace the html using rjs template. Here are some code for you reference:

email.html.erb called on www.example.com

   <div id="div_content">
    <%= form_for(@user, :url => "http://data.example.com/mail", :remote => true) do |f| %>
    <%= f.label :email %>
    <%= f.text_field :email%>
    <% end %>
   </div>

email action of application on : data.example.com/email -

def email
  @user = User.create(params[:user])
  respond_to do |format|
    if @user.save!
       format.html { redirect_to(user_page_path(@user.vip_id), :notice => 'Thank you! You are now on our priority list.') }
       format.js
    else
       format.html { render :text => "user can not be saved at this moment!"}
    end
  end
end

email.js.rjs called on www.data.example.com/email

page.replace_html :div_content, :partial => "show", :object => @user

I can see in my log that request comes all the way from one domain to sub domain and even action gets triggered but, I can not get the response back to the main domain. So, is there any way to send a callback to main domain. I just want to reflect changes there at the form which is inside div_content div and want to replace with content of _show.html.erb which I have on my sub domain.

Many Thanks,
Surya :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-12-06 09:52:11

发生这种情况是因为 Rails 认为您正在尝试对自己发起跨站点请求伪造攻击。默认情况下,rails 有一个安全功能,可以拒绝从外部源(即您的其他应用程序)提交表单。

最简单(但不是最安全)的解决方法是将其添加到您发布数据的控制器的顶部回复:

protect_from_forgery :except => :email

回复评论

啊,我明白了,当我第一次读到你的帖子时,我并没有非常仔细地关注,对此感到抱歉。我错过了所有关于 rjs 的部分。

我当然不是 rjs 方面的专家,但看起来你做的几乎所有事情都是正确的。对我来说唯一可疑的部分是这一行:

page.replace_html :div_content, :partial => "show", :object => @user

我认为应该是:

page.insert_html(:bottom, "div_content", :partial => "show")

另外,您可能想尝试用

page.alert("debug");

Just 替换 rjs 模板,以确保它真的不会回来,因为我怀疑它是......

This is happening because rails thinks you are trying to launch a cross site request forgery attack against yourself. By default rails has a security feature baked in that rejects form submission from outside sources (ie your other app)

Easiest (but not the most secure), way around it would be to add this to the top of the controller you are posting the data to:

protect_from_forgery :except => :email

RE COMMENTS

Ahh I see, I was not paying very close attention when I first read your post, sorry about that. I missed all the parts about rjs.

I am certainly no expert on rjs but it looks like you are doing most everything right. Only suspicious part to me is this line:

page.replace_html :div_content, :partial => "show", :object => @user

I think it should be:

page.insert_html(:bottom, "div_content", :partial => "show")

Also you might want to try and replace the rjs template with

page.alert("debug");

Just to make sure its really not coming back, because I would suspect it is...

迷雾森÷林ヴ 2024-12-06 09:52:11

伙计们,这就是我让它工作的方法 -

好吧,我正在尝试使用我的两个 RoR 应用程序进行跨域通信,正如我在问题中提到的那样!

最后我找到了一种使用“EasyXDM”(www.easyxdm.net)实现相同目标的方法,它解决了问题。它适用于大多数浏览器,包括 IE7 和 Firefox 旧版本。
如果你想完成它,CORS 是另一种解决方案,但它无法在 IE7 上工作......

哇!现在我可以依赖不同应用程序之间的跨域通信,而无需使用 iFrame。

Guys here is what I got to make it work -

Well I was trying to do cross domain communication using my both RoR app as I mentioned right up there in my question!

Finally I have found a way to achieve the same using "EasyXDM" (www.easyxdm.net) it solved the problem. It works great on most of the browsers including IE7 and Firefox older versions.
CORS is another solution if you want to get it done, but it fails to work on IE7...

whoa! now I can rely on the cross domain communication between my different apps without using an iFrame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文