rails - “WARNING: Can't verify CSRF token authenticity” for json devise requests

前端 未结 10 2198
终归单人心
终归单人心 2020-11-27 10:11

How can I retrieve the CSRF token to pass with a JSON request?

I know that for security reasons Rails is checking the CSRF token on all the request types (including

10条回答
  •  误落风尘
    2020-11-27 10:45

    I resolved that error this way:

    class ApplicationController < ActionController::Base
      protect_from_forgery
      skip_before_action :verify_authenticity_token, if: :json_request?
    
      protected
    
      def json_request?
        request.format.json?
      end
    end
    

    Source: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html

提交回复
热议问题