【发布时间】:2014-08-14 18:35:16
【问题描述】:
我在 Rails 网站上有 ruby。 ApplicationController 看起来像
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :handle_cookie
DEFAULT_MARKETPLACE = 1
def handle_cookie
if (session[:isImpersonation].nil?)
puts "initilized to 0"
session[:isImpersonation] = 0
end
end
如果变量 isImpersonation 为 nil,我将在会话对象中初始化它。当发出正常请求(即非 ajax)时,变量会被初始化一次,即会话对象保留变量 isImpersonation。
但是当我像这样进行 ajax 调用时:
$.ajax({
url : "/a/fetch",
type : "POST",
data : {
runDate : $('#rundate_select :selected').val(),
id : idVal,
mp : mpVal
},
beforeSend : function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]')
.attr('content'));
},
success : function(data) {
var $elem = $('<div>').html(data);
$("#overall").html(
$elem.find('#overall').html());
},
error : function(data) {
}
});
在此 ajax 调用期间,会话中的变量被重新初始化。我搜索并关注了该帖子: Rails not reloading session on ajax post.
但是,它对我不起作用。知道我缺少什么吗?
【问题讨论】:
-
当
ajax拨打电话时,log会说什么?
标签: ruby-on-rails ruby-on-rails-3 session