gpt4 book ai didi

PHP header() 在调用 ajax 后不重定向

转载 作者:可可西里 更新时间:2023-11-01 14:03:02 25 4
gpt4 key购买 nike

我正在使用ajax 来插入数据。注册成功后,它将重定向到其注册页面或 registry.php。但它似乎没有重定向到其各自的页面,奇怪的是当我在 firefox 的控制台上检查它时,它似乎给出了正确的结果,类似于 POST xxxx/form_zuri/insertData.php 302 Found50 毫秒和 GET xxxx/form_zuri/registry.php 302 发现 37 毫秒。

这是 insertData.php 的代码

    if($data = $admin->insertRegistrantInfo($fname, $lname, $aname, $email, $password) == true) {
session_regenerate_id(true);// destroying the old session id and creating a new one
$_SESSION['id'] = $data;
header('location: registry.php');
exit();

这是我的 ajax 脚本:

$("#submit_third").click(function(){


$.ajax({
type: "POST",
url: "insertData.php",
data: {

fname : $("#fname").val(),
lname : $("#lname").val(),
aname : $("#aname").val(),
email : $("#email").val(),
password : $("#password").val()

},
cache: false,
success: function(data) {

//window.location.replace('registry.php') ;

}
});
//window.location.replace('registry.php') ;
//return false;

});

如果我将使用 .replace js 函数,它将无法获取 SESSION ID。

有什么想法吗?

最佳答案

当您在 AJAX 中调用 PHP 脚本时,请求由 AJAX 调用执行,而不是客户端所在的当前页面,因此它不会更改实际正在查看的页面上的标题 - 仅在AJAX 调用的响应。在这种情况下,这不会做你想做的。

此外,当内容已经输出时,您不能更改 header ,在您的情况下,它会再次输出。

如果您在 AJAX 回调中使用 window.location.href = 'registry.php', session ID 应该在那里。使用 window.location.replace 不会在您的历史记录中添加新记录。

关于PHP header() 在调用 ajax 后不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com