我发送AJAX POST request
到handleSession.php
文件,该文件用于获取POST参数,然后将它们添加到会话中。但是,handleSession.php file fails to update the session data
。这是AJAX request code
正常工作
var req = JSON.stringify({
body:body,
img:img,
title:title,
id:id,
category:category}
);
fetch("handleSession.php", {
method:"POST",
body: req
})
.then((data) => data.text())
.then(data => JSON.parse(data))
.then(data => console.log(data))
.catch((err) => console.log(err))
下面是handleSession.php文件,该文件应该将$_POST
数据解析为会话
<?php
ob_start();
session_start();
$data = json_decode(file_get_contents('php://input'), true);
$arr = array(
"title" => "",
"img" => "",
"body" => "",
"id" => "",
"category" => ""
);
foreach ($data as $key => $value) {
$arr[$key] = $data[$key];
}
$_SESSION["fav"] = $arr["title"];
//even if I say $_SESSION["FOO"] = "bar"; it still doesn't work
echo json_encode($arr);
?>
因此,在页面顶部session_start()
和ob_start()
的另一个页面上,我已经说过
if(isset($_SESSION["fav"])){
echo $_SESSION["fav"];
}else{
echo "Session fav not set";
}
它总会进入其他条件
任何帮助表示赞赏!