簡體   English   中英

codeigniter ajax請求與會話

[英]codeigniter ajax request with session

無法通過ajax請求獲取會話值

這是我的ajax req

$.post(url, payload, 
    function(returnedData){
        console.log(returnedData); //does not print session values it seems session has restarted
         var jsonData = JSON.parse(returnedData);

         $('#cart').empty().append(jsonData.data);
});

在console.log中輸出

    Array
(
    [__ci_last_regenerate] => 1473660791
)

分配會話變量

public function test($id=null)
{
    $_SESSION['tot']=1;
}

測試不使用ajax

public function addToCart($id=null) //note that this is also the control for my ajax req
{            
        print_r($_SESSION);
        die('0');
}
public function test1()
{
    print_r($_SESSION); // it prints fine
}

產量

Array ( [__ci_last_regenerate] => 1473660731 [tot] => 1 )

我自動加載了我的會話庫。 CI表示,只要它存在,使用會話魔法或php訪問會話變量的方式並不重要。

你好,你可以使用默認的codeignitor會話,請提一下你做的ajax請求和ajax請求的php代碼的url

喜歡

$this->session->set_userdata('tot',1);

然后像這樣打印會話數組

echo "<pre">;print_r($this->session->userdata('tot'));

你的代碼應該是

 public function test($id=null)
    {
        $this->session->set_userdata('tot', 1);
        //instead $_SESSION['tot']=1;
    }
    public function addToCart($id=null) //note that this is also the control for my ajax req
    {            
            $session_id = $this->session->userdata('session_id');
echo $session_id;
    }
    public function test1()
    {
        //print_r($_SESSION); // it prints fine
$session_id = $this->session->userdata('session_id');
echo $session_id;
    }

並確保加載會話庫($ this-> load-> library('session');)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM