无法通过 Ajax 将表单发布到不同的子域(警告:无法验证 CSRF 令牌的真实性)
我尝试将远程表单从表单之一(domain.com)发送到不同的子域(example.domain.com),但我不断收到警告警告:无法验证 CSRF 令牌真实性 在日志中,chrome 中的检查器告诉我请求的状态是
(canceled)
,类型为 application/x-www-form-urlencoded
。
这是形式: button_to "Follow", follow_users_url(subdomain: post.user_username_slug), remote: true
当我删除 remote: true
时,我得到了我想要的结果。另外,当尝试在操作的同一子域(example.domain.com)中使用相同的表单时,我得到了正确的结果。
我找到了一种在所有子域中共享 cookie 的方法(session_store.rb
中的 domain: :all
),但我找不到在 Ajax 中共享令牌的方法请求。
我正在使用 Rails 3.1.3、Ruby 1.9.3 和 jQuery 1.7.1。
任何人都可以帮助我吗?
编辑:
该问题似乎与CORS有关。现在我正在尝试找到最小摩擦解决方案以使此(异步)跨子域请求正常工作。
I trying to send a remote form to a different subdomain (example.domain.com) from the one of the form (domain.com), but i keep getting the warn WARNING: Can't verify CSRF token authenticity
in the log, and inspector in chrome tells me that the status of the request is (canceled)
, with the type application/x-www-form-urlencoded
.
This is the form:button_to "Follow", follow_users_url(subdomain: post.user_username_slug), remote: true
When i remove the remote: true
, i got the result i was hopping for. Also, when a try to use the same form in the same subdomain of the action (example.domain.com), i got the correct result.
I found a way to share the cookies in all subdomains (domain: :all
in session_store.rb
), but i can't found a way to share the token in Ajax requests.
I'm using Rails 3.1.3, Ruby 1.9.3 and jQuery 1.7.1.
Anyone can help me, please?
Edit:
The problem seems to related to CORS. Now i'm trying to find a minimum friction solution to make this (Asynchronous) cross subdomains requests work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 POST 参数中,包含字段“authenticity_token”以及助手 *form_authenticity_token* 返回的值。 (与cookie无关)。
编辑 我认为您遇到了同源策略,该策略阻止域 A 中的 javascript 与域 B 进行通信(也适用于子域)。有一个名为 CORS 的“覆盖”,您正在交谈的域必须实现它。
因此,假设您可以控制域 A 和 B,则可以解决此限制。这解释了为什么“正常”请求有效,而“:remote => true”请求无效。 (CSRF 令牌错误可能不准确。)这是一篇有关在 Rails 中设置 CORS 的文章(域 B,在我的示例中)。
In your POST paramaters, include the field "authenticity_token" with the value returned by the helper *form_authenticity_token*. (It has nothing to do with cookies).
Edit I think you're hitting up against the Same-Origin Policy, which prevents javascript from domain A from communicating with domain B (also applies to subdomains). There is an "override" for this called CORS, which the domain you are talking to must implement.
So assuming you have control over both domain A and B, you can work around this limitation. This explains why "normal" requests work while ":remote => true" requests don't. (The CSRF token error is probably inaccurate.) Here's an article about setting up CORS in Rails (domain B, in my example).
您可以将两个控制器中的真实性令牌设置为相同
我认为它是
protected_from_forger :secret => 'long_secret_string'
如果两个控制器使用相同的令牌,您应该能够跨子域或其他站点发布。不过,您确实打开了一些跨站点脚本漏洞
You can set the authenticity token in both controllers to be the same
I think it is
protect_from_forger :secret => 'long_secret_string'
If both controller use the same token you should be able to post across sub domains or other sites. You do open up some cross site scripting holes though