请注意,我在这两个php文件中的任何代码之前使用session_start();
:
最小化格式的问题:
index.php
<?php
session_start();
$_SESSION['msg'] = 'hello';
?>
在index.php中使用Ajax调用add_to_cart.php时:
$.ajax({
type:"POST",
url:"add_to_cart.php",
async: false, //tested with this true and now false
data:{test_product:productName},
success:function(response){
alert(response);
}
});
add_to_cart.php:
<?php
session_start();
echo json_encode($_SESSION['msg']);
?>
警报的输出为null
我正在ubuntu上的nginx上运行php 7.4。
答案 0 :(得分:0)
请尝试这个。
从index.php中删除$_SESSION['cart'] = array();
然后按照以下add_to_cart.php中的说明将项目保存在购物车中。
$_SESSION['cart'][] = [$productName,$productQty];
答案 1 :(得分:0)
我已经在另一台运行Apache而不是Nginx的计算机上进行了测试,并且可以正常工作。在nginx上,ajax php文件上的session的var_dump立即为空,但是在apache服务器上内容完整。