Home  >  Q&A  >  body text

ruby - Can't verify CSRF token authenticity

问题:

同样的接口请求,使用get工作正常。
使用post请求提示:
Can't verify CSRF token authenticity

临时的解决方法

在controller 中跳过验证

skip_before_action :verify_authenticity_token, :only => ["your_update_action"]

http://stackoverflow.com/questions/19390389/using-curl-to-access-backend-api-of-rails-app-that-implements-devise

后续疑问

如何通过post请求发送token验证?

高洛峰高洛峰2709 days ago950

reply all(3)I'll reply

  • 阿神

    阿神2017-04-24 16:01:32

    Get does not verify csrf. Is your csrf_token placed in the request body?

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-04-24 16:01:32

    Usually called in the layout file<%= csrf_meta_tags %> 方法,并在js中引入 jquery_ujs

    For example

    application.erb

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>you title</title>
      .....
      <%= csrf_meta_tags %>
    </head>
    <body>
    </body>
    </html>
    

    application.js

    #= require jquery_ujs

    Gemfile

    gem 'rails-jquery'

    reply
    0
  • 高洛峰

    高洛峰2017-04-24 16:01:32

    Is it included in the form or added to the ajax request body? From your description, it should be that this csrf parameter is not added to the body of the post request

    reply
    0
  • Cancelreply