【问题标题】:Ajax PUT request results in logout in Rails app. Why?Ajax PUT 请求导致 Rails 应用程序注销。为什么?
【发布时间】:2012-03-29 23:11:21
【问题描述】:

my Rails app 中的 2 个地方有 ajax 调用。 (一种用于 jQueryUI 拖放排序,一种用于更新评论帖子)。

每当发生这些调用时,用户都会被注销。没有明显的原因。 我正在使用omniauth-facebook 和omniauth-google-oauth2 进行身份验证。

如何解决这个问题?

下面是 ajax 调用的样子(coffeescript):

  $.ajax({
    type: 'put',
    data: {post_id: post.attr("id")},
    dataType: 'json',
    complete: -> post.children('.headpost').children('.buttons').removeClass('new_reply'),
    url: '/posts/update/'})

谢谢!

【问题讨论】:

  • weblog.rubyonrails.org/2011/2/8/… 好的,会话被重置,因为我的 ajax 请求没有真实性令牌。现在,如何使用资产咖啡脚本文件中的 ajax 请求来做到这一点...

标签: ajax ruby-on-rails-3 session csrf logout


【解决方案1】:

我最终做了什么:

在application.html.erb布局头中,<%= csrf_meta_tags %>下:

<%= javascript_tag "var AUTH_TOKEN = '#{form_authenticity_token}';" if protect_against_forgery?%>

在 assets/whatever.js.coffee 中

$.ajax ({
            type: 'put',
            data: {authenticity_token: AUTH_TOKEN},
            dataType: 'json',
            complete: -> post.children('.headpost').children('.buttons').removeClass('new_reply'),
            url: '/posts/'+post.attr("id").slice(5) });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 2014-07-28
相关资源
最近更新 更多