导轨不重装会议阿贾克斯岗位上 [英] Rails not reloading session on ajax post

查看:104
本文介绍了导轨不重装会议阿贾克斯岗位上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到使用jQuery一个很奇怪的问题,Rails和AJAX(虽然我不认为这是具体到jQuery的)。

I'm experiencing a very strange problem with Rails and ajax using jQuery (although I don't think it's specific to jQuery).

我的Rails应用程序使用的cookie会话存储,我有一个非常简单的登录,设置在会话的用户ID。如果user_id是不是在会话设置,它重定向到登录页面。这工作没有任何问题。 JQuery的GET请求正常工作了。问题是,当我做一个jQuery POST - 浏览器发送会话cookie OK(我证实了这一点与萤火虫和倾倒Request.Cookies时到日志),但会是空白的,即会话{}

My Rails application uses the cookie session store, and I have a very simple login that sets the user id in the session. If the user_id isn't set in the session, it redirects to a login page. This works with no problems. JQuery GET requests work fine too. The problem is when I do a jQuery POST - the browser sends the session cookie ok (I confirmed this with Firebug and dumping request.cookies to the log) but the session is blank, i.e. session is {}.

我在做这在我的application.js:

I'm doing this in my application.js:

$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr('content');
  xhr.setRequestHeader('X-CSRF-Token', token);
});

和这里是我的样品后:

$.post('/test/1', { _method: 'delete' }, null, 'json');

这应该得到这个控制器方法(_method:删除):

which should get to this controller method (_method: delete):

def destroy
  respond_to do |format|
    format.json { render :json => { :destroyed => 'ok' }.to_json }
  end
end

查看日志和使用萤火虫,我可以确认正确的cookie的值在请求头当AJAX后发生发,但似乎在某些时候的Rails失去这个价值,因此失​​去了会话,所以它重定向到登录页面,并不会继续执行方法。

Looking at the log and using Firebug I can confirm that the correct cookie value is sent in the request header when the ajax post occurs, but it seems that at some point Rails loses this value and therefore loses the session, so it redirects to the login page and never gets to the method.

我已经试过所有我能想到的调试这一点,但我在我身边的想法,这可能是个Rails的一个错误。我用Rails 3.0.4和jQuery 1.5是否有帮助。我觉得很奇怪,经常(即非Ajax)GET和POST请求的工作,和Ajax get请求,没有任何问题的工作,它只是阿贾克斯的职位没有。

I've tried everything I can think of to debug this but I'm coming around to the idea that this might be a bug in Rails. I'm using Rails 3.0.4 and jQuery 1.5 if that helps. I find it very strange that regular (i.e. non-ajax) get and post requests work, and ajax get requests work with no problems, it's just the ajax posts that don't.

任何帮助,试图解决这一问题将是极大的AP preciated!

Any help in trying to fix this would be greatly appreciated!

非常感谢,
戴夫

Many thanks,
Dave

推荐答案

我要回答我的问题,我已经成功地制定出了事情的原委。我会后在这里的情况下是非常有用的其他人!

I'm going to answer my own question as I've managed to work out what was going on. I'll post it here in case it's useful to anyone else!

进一步调查之后,我摸索出了code,这是的应该的设置请求头的CSRF令牌,是不是。这是原来的code:

After investigating further, I worked out that the code that was supposed to be setting the request header with the CSRF token, wasn't. This was the original code:

$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr('content');
  xhr.setRequestHeader('X-CSRF-Token', token);
});

发生了什么事是,这个code没有设置标题,Rails的正在接受Ajax请求,该令牌不匹配,这是复位会话。这曾经引发的ActionController :: InvalidAuthenticityToken错误(我想我会抓住了这个早期,如果有人提出一个错误......哦),但由于Rails的3.0.4,现在只是静静地重置了会议。

What was happening was that this code wasn't setting the header, Rails was receiving an Ajax request, the token didn't match and it was resetting the session. This used to raise an ActionController::InvalidAuthenticityToken error (I suppose I would have caught this earlier if an error was raised... oh well), but since Rails 3.0.4 it now just quietly resets the session.

所以令牌发送的头,你要做到这一点(很多感谢这个奇妙的博客帖子):

So to send the token in the header, you have to do this (many thanks to this marvellous blog post):

$.ajaxSetup({
  beforeSend: function(xhr) {
    xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
  }
}); 

而现在所有的作品,因为它应该。这是很好的。

And now it all works as it should. Which is nice.

这篇关于导轨不重装会议阿贾克斯岗位上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆