这是我的javascript代码,成功事件被执行。但是 post 数组在 php 文件中保持为空。 document.querySelector(".postButtons").addEventListener("click",(e)=>{ const postID = e.target.value; if(e.target.id == "edit"){ //Send post request to edit the post $.ajax({ type: 'POST', url: '../edit.php', data: {postID : postID}, contentType: 'application/json; charset=utf-8', data: 'json', cache: false, success: function(response) { window.location.href="../edit.php" }, error: function(err){ console.log(err) } }) }else if(e.target.id == "del"){ //Send post request to delete the post }}) <?phpif(isset($_POST["postID"])){ echo $_POST["postID"];} ?>我试图在javascript中将变量记录到控制台,它似乎工作。当我试图返回它时,状态码是200。
1 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
window.location.href 执行常规 GET 请求。因此,您在页面上看不到 postID。您可以在请求中的控制台中看到 postID
但是如果你想在页面重新加载时看到 postID,那么你需要使用表单发送这个请求,而不是 ajax。
<form action="../edit.php" method="POST" id="sendForm">
<input type="hidden" name="postID" id="postID">
<input type="submit">
</form>
if(e.target.id == "edit"){
$('#postID').val(postID);
$('#sendForm').submit();
}
添加回答
举报
0/150
提交
取消
