简体   繁体   English

Rails 3:无法验证CSRF令牌的真实性

[英]Rails 3 : Can't verify CSRF token authenticity

When i tried to post data through my REST Client ,i am getting a warning like this 当我试图通过我的REST客户端发布数据时,我收到这样的警告

Warning :Can't verify CSRF token authenticity.

How to solve this. 怎么解决这个问题。

I believe you are trying to make a POST from a link . 我相信你正试图从一个link一个POST By default links aren't supposed to make POST requests, only GET ones. 默认情况下, links不应该发出POST请求,只能发出GET请求。 So when you try to make the connection to your server, Rails warns you about this. 因此,当您尝试建立与服务器的连接时, Rails向您发出警告。
One way to bypass this kind of behavior is, instead of using a link , use a form . 绕过这种行为的一种方法是使用form ,而不是使用link So you can make proper POST requests to the server. 因此,您可以向服务器发出正确的POST请求。
Alternatively, you can remove the protect_from_forgery line from your application_controller , so Rails doesn't do this kind of verification, but this is extremely not recommended. 或者,您可以从application_controller删除protect_from_forgery行,因此Rails不会执行此类验证,但我们不建议这样做。

You could do this, (but not recommended :)), skip forgery protection 你可以这样做,(但不推荐:)),跳过伪造保护

Ex: you are posting data to PostController => Create action 例如:您要将数据发布到PostController =>创建操作

class PostsController < ApplicationController

   before_filter :protect_from_forgery, :except => [:create]  

   def create
        #your method
   end
end

but having said that, I'm sure there should be a better way to do what you want to do, so if you could explain what you want to do, someone could help 但话说回来,我确信应该有更好的方法来做你想做的事,所以如果你能解释你想做什么,有人可以帮忙

HTH HTH

nop NOP

I missed the following line in my application.js 我错过了我的application.js中的以下行

//= require jquery_ujs

I replaced it and its working.. 我替换它和它的工作..

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

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