简体   繁体   English

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

[英]How to make an AJAX call to different domains in Ruby on Rails 3.0

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. 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以js格式保存数据。 But I want to send back the acknowledgement to www.example.com and replace the html using rjs template.但我想将确认发送回 www.example.com 并使用rjs模板替换 html。 Here are some code for you reference:以下是一些代码供您参考:

email.html.erb called on www.example.com email.html.erbwww.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 - email应用程序操作: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 email.js.rjswww.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.我只想在div_content div 内的表单中反映更改,并希望用我的子域上的_show.html.erb的内容替换。

Many Thanks,非常感谢,
Surya:)苏里亚:)

This is happening because rails thinks you are trying to launch a cross site request forgery attack against yourself.发生这种情况是因为 Rails 认为您正试图对自己发起跨站点请求伪造攻击。 By default rails has a security feature baked in that rejects form submission from outside sources (ie your other app)默认情况下,rails 有一个安全功能,它拒绝来自外部来源(即您的其他应用程序)的表单提交

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:最简单(但不是最安全)的解决方法是将其添加到 controller 的顶部,您将数据发布到:

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.我错过了关于 rjs 的所有部分。

I am certainly no expert on rjs but it looks like you are doing most everything right.我当然不是 rjs 方面的专家,但看起来你做的大部分事情都是正确的。 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此外,您可能想尝试将 rjs 模板替换为

page.alert("debug");

Just to make sure its really not coming back, because I would suspect it is...只是为了确保它真的不会回来,因为我怀疑它是......

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

Finally I have found a way to achieve the same using "EasyXDM" (www.easyxdm.net) it solved the problem.最后,我找到了一种使用“EasyXDM”(www.easyxdm.net)实现相同的方法,它解决了这个问题。 It works great on most of the browsers including IE7 and Firefox older versions.它适用于大多数浏览器,包括 IE7 和 Firefox 旧版本。 CORS is another solution if you want to get it done, but it fails to work on IE7...如果您想完成 CORS 是另一种解决方案,但它无法在 IE7 上运行...

whoa.哇。 now I can rely on the cross domain communication between my different apps without using an iFrame.现在我可以依靠我的不同应用程序之间的跨域通信而无需使用 iFrame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM