我在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
如果它是nil,我正在初始化会话对象中的变量isImpersonation。当正常请求(即非ajax)时,变量被初始化一次,即会话对象保留变量isImpersonation。
但是,当我打电话时,如:
$.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
但是,它不适合我。知道我错过了什么吗?
答案 0 :(得分:0)
最后,我得到了它的工作。我失踪了:
<%= csrf_meta_tags %>
在我的布局文件的标记中,即application.html.erb