HTML Javascript $_post 响应显示 PHP 标头位置页面,而不是转发



我有一个html代码,它将输入发送到PHP,php重定向到登录页面。

但是当我将 post 请求作为 bellow 时,它会向 javascript 提供响应;使用转发索引.html代码而不重定向到索引.html。

<script>
function Sendmeaway() {
var item_i = document.getElementById("item_select").value;
if (item_i == "Initial_Value") {
alert("Please Select the Correct Option or Enter the Comment");
} else {
$.post("./back_p/add_change_req.php", {
item : item_i
},
function(data, textStatus) {
alert("Response from server: " + data);
document.getElementById("item_select").selectedIndex = 0;
location.reload();
});
}
};
</script>

PHP代码(当我直接进入PHP时,它可以正常工作)

<?php echo $_POST["item"]; header('location: ../index.html'); ?>

你不能像这样重定向用户。AJAX调用只能发送和接收数据。

而是在AJAX调用的成功功能中使用window.location.href = ''重定向用户。

所以而不是

<?php echo $_POST["item"]; header('location: ../index.html'); ?>

<?php echo $_POST["item"]; return '../index.html'; ?>

然后在script中使用该返回值,如下所示:

function(data, textStatus) {
alert("Response from server: " + data);
document.getElementById("item_select").selectedIndex = 0;
window.location.href = data; 
});

当你进行ajax调用时,你需要像这样改变javascript端的页面。您可以从 php 发送 0 或 1 来决定是否重定向。

不要使用php 标头,而是在 PHP 标签外使用<meta>标签

如:

<?php
echo($_POST["item"]);
?>
<meta http-equiv="refresh" content="5;URL='../Index.html'" />

在执行重定向之前,content="到所需的秒数后替换 5

最新更新