不确定这是否可行,但我有一个使用 AJAX 提交表单的页面,如果满足某些条件,它应该自动将用户带到另一个页面。 header 标记之前不会输出任何内容,它只是一堆条件。
问题: header 重定向不起作用...
AJAX
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '_ajax/add.php',
data: $('form').serialize(),
success: function (data) {
$("input").val('Company Name');
$("form").hide();
getInfo();
}
});
});
add.php
$row = mysqli_fetch_array($result);
$id = $row['id'];
header("Location: http://localhost/manage/card.php?id=$id");
最佳答案
header 只能在任何正文发送到浏览器之前进行修改(因此名称为 header /正文)。由于您已将 AJAX 发送到浏览器,因此您无法再修改 header 。但是,您可以让通过 AJAX 调用的 add.php
脚本返回 $id
参数。然后可以在 JavaScript 中使用该参数来重定向页面:window.location = 'http://localhost/manage/card.php?id=' + id
。
有关 PHP header()
的更多信息:http://www.php.net/manual/en/function.header.php
AJAX
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '_ajax/add.php',
data: $('form').serialize(),
success: function (data) {
window.location = 'http://localhost/manage/card.php?id=' + data;
}
});
});
add.php
$row = mysqli_fetch_array($result);
$id = $row['id'];
echo $id;
exit;
关于javascript - 通过ajax传递时 header 不会重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20938989/