2014-06-24 28 views
0

我在rails網站上有ruby。在ApplicationController中看起來像在rails應用程序中發出ajax調用時會話被破壞

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如果是零。當進行正常的請求(即非ajax)時,變量被初始化一次,即會話對象保留變量isImpersonation。

但是,當我讓Ajax調用,如:

$.ajax({ 
    url : "https://stackoverflow.com/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

什麼'log'說,當'ajax'調用時? – Nithin

回答

0

最後,我得到它的工作。我失蹤:

<%= csrf_meta_tags %> 

在我的佈局文件的標籤即application.html.erb

相關問題