Rails - 如何向用 javascript 创建的表单添加 CSRF 保护? [英] Rails - How to add CSRF Protection to forms created in javascript?
问题描述
我正在使用backbone.js,效果很好.但是我作为 javascript 模板创建的表单缺少 rails csrf 保护令牌.如何将其添加到我在 javascript 中创建的模板中?
I'm using backbone.js and it works great. but the forms I'm creating as a javascript template lacks the rails csrf protection token. How do I add it to templates I'm creating in javascript?
推荐答案
如果您的布局中有 <%= csrf_meta_tag %>
并且您可以从 js 访问它,那么您可以使用 $('meta[name="csrf-token"]')
If you have <%= csrf_meta_tag %>
in your layout somewhere and that is accessible to you from the js, then you can access it using $('meta[name="csrf-token"]')
参见 http://eunikorn.blogspot.com/2011/07/working-with-backbonejs-in-harmony-with.html 了解如何在每个主干请求中加入 csrf 支持
See http://eunikorn.blogspot.com/2011/07/working-with-backbonejs-in-harmony-with.html for an idea on how to hack in csrf support into each backbone request
这篇关于Rails - 如何向用 javascript 创建的表单添加 CSRF 保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!