jQuery Ajax PHP重定向到另一个页面

JavaScript文件:

$.ajax({ type: "POST", url: "ajax.php", data: dataString, success: function(r) { $("#div").html(r); } }); 

我想在成功条件下将页面重定向到new.php ,所以在ajax.php文件中我使用了header('Location:new.php'); 但不是重定向到页面new.phpnew.phpdiv加载new.php的内容。

为了将现有页面重定向到整个新页面,我需要进行哪些更改?

 $.ajax({ type: "POST", url: "ajax.php", data: dataString, success: function(r) { window.location = 'new.php';//window.location.href = 'new.php'; //$("#div").html(r); }, }); 

通过在您通过AJAX请求的文件中放置标题,您只需将AJAX请求重新路由到该URL即可。 您不会重定向用户的浏览器,因为AJAX请求发生在后台。

相反,您需要在JavaScript文件中进行重定向,特别是在success子句中:

 //From Pragnesh Chauhan's answer (modified): $.ajax({ type: "POST", url: "ajax.php", data: dataString, success: function(r) { window.location.href = 'new.php'; //$("#div").html(r); Useless since you're redirecting. }, }); 

我不确定为什么你甚至想要使用jquery / ajax来实现这个function,因为使用ajax的重点是能够重新加载页面的一部分而无需刷新整个页面或重定向到新页面。

为什么不将表单数据发布到同一页面并使用PHP执行所需操作然后使用PHP重定向到new.php或将表单发​​布到new.php并对该页面上发布的数据执行所需操作。

 window.location.href = "new.php"; 

试试这个success()