[英]Why does PHP does not receive the post request that made by AJAX
這是我的 javascript 代碼,成功事件被執行。 然而,php 文件中的 post 數組仍然為空。
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
}
})
<?php
if(isset($_POST["postID"])){
echo $_POST["postID"];
}
?>
我試圖在 javascript 中將變量記錄到控制台,它似乎工作。當我試圖返回它時,狀態碼是 200。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.