【问题标题】:Rails: CSRF token not working but setupRails:CSRF 令牌不起作用但已设置
【发布时间】:2013-10-25 16:27:30
【问题描述】:

我在 heroku 上有我的 rails 3 应用程序,当我发送银行信息时,我得到:WARNING: Can't verify CSRF token authenticity 但我的 CSRF 令牌已设置。 https://gist.github.com/anonymous/7081401

$.ajax({ url: '#{credit_cards_path}', 
type: 'POST',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
dataType: "json",
data: { cc_uri: response.data.uri, 
        address: $('.address').val()
        // etc ...
      },
success: function(randomobject) {
  window.location = '/products/' + randomobject.value + '/receipt';
  }
});

heroku 日志

829962+00:00 app[web.1]: Started POST "/products/2/card" for 100.2.109.97 at 2013-10-21 09:13:03 +0000
835379+00:00 app[web.1]: WARNING: Can't verify CSRF token authenticity
834929+00:00 app[web.1]: Processing by CreditcardsController#addcard as HTML
834929+00:00 app[web.1]: Parameters: {"state"=>"NY", "id"=>"2"}
604099+00:00 heroku[router]: at=info method=POST path=/products/2/card host=app.herokuapp.com fwd="100.2.109.97" dyno=web.1 connect=1ms service=1781ms status=500 bytes=643
604259+00:00 app[web.1]:
602739+00:00 app[web.1]: Completed 500 Internal Server Error in 1768ms
604259+00:00 app[web.1]: Balanced::BadRequest (Balanced::BadRequest(400)::Bad Request:: POST https://api.balancedpayments.com/v1/customers: request: Invalid field [card_uri] - "None" must be a string URI Your request id is OHMfe86f2883a3011e3980d02a1fe53e539. ):

【问题讨论】:

  • 你能确认#{form_authenticity_token}的值在编译后的javascript中输出正确吗?
  • 那是编译好的javascript吗?像一个 js.erb 文件?您是否为每个请求重新生成它?

标签: ruby-on-rails ruby heroku


【解决方案1】:

可能与您使用#{form_authenticity_token}有关

根据这个来源WARNING: Can't verify CSRF token authenticity rails你应该可以使用这个代码:

headers: {
  'X-Transaction': 'POST Example',
  'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},

【讨论】:

  • 它已经到了那里,但我可以看到另一个问题。您正在尝试在 .js 文件中包含 ruby​​ 变量。在尝试我的解决方案之前,请尝试将您的 balanced.js 更改为 balanced.js.erb 并查看原始代码会发生什么
  • 啊解决了!!原来我的 javascript 并没有真正进入我似乎忘记输入的浏览器 //=require myjs.js
  • 哈哈!我总是发现人们在 SO 上提出问题,但他们的问题实际上与他们遇到的问题无关!如果您有任何进一步的要求,请告诉我!
猜你喜欢
  • 2020-03-23
  • 2012-08-02
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2015-05-05
  • 1970-01-01
相关资源
最近更新 更多