簡體   English   中英

帶有會話的ajax php發布不會給出結果

[英]ajax php post with sessions wont give results

我試圖通過ajax獲取預設的會話變量,並在我的模式中顯示。 它像一個購物車。 但是,當我嘗試獲取它們時,當我從chrome inspect network或modal讀取ajax結果時根本沒有結果。 下面是我的代碼。 我在這里做錯了什么?

<script>
    $("#cart-button").click(function(){
        $.ajax({
            url: "includes/cart-read.php",
            success: function(data){

                  console.log(data);
                  alert(data);
                $('#modal-body').empty().append(''+data+'');
            }
        });
    });
</script>

並在cart-read.php中

if(isset($_SESSION['dices'])){
    foreach ($_SESSION['dices'] as $dice){

        $msg = $dice;

        echo json_encode($msg);

    }
}

會話骰子是具有簡單數字的數組。 例如4、5、6

確保代碼中包含以下內容

  1. 檢查是否添加了session_start(); 下面之后
  2. 嘗試在會話檢查中添加其他情況,
  3. 嘗試使用數組來存儲和顯示結果。

     <?php session_start(); $result = array(); $msg = array(); if(isset($_SESSION['dices'])){ foreach ($_SESSION['dices'] as $dice){ array_push($msg,$dice); } if(sizeof($msg) > 0) { $result['status'] = true; $result['message'] = $msg; } else { $result['status'] = false; $result['message'] = 'No values'; } } else { $result['status'] = false; $result['message'] = 'Session not set'; } echo json_encode($result); ?> 

暫無
暫無

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

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