jquery发布完成后

Sco*_*ott 0 ajax jquery post redirect

我有一个脚本,它执行jquery AJAX帖子,然后应该重定向到另一个页面.问题是它有时会在帖子完成之前重定向.似乎有几个解决方案,但我不能让它们中的任何一个起作用.谢谢你的帮助!这是我的代码:

$.post("cart.php", { 'products[]':postvalues }, function(data) { });
location.href="index.php";
Run Code Online (Sandbox Code Playgroud)

TM.*_*TM. 5

你需要把你的location.href内部回调函数.

$.post("cart.php", { 'products[]':postvalues }, function(data, status) {
    // if you don't want to redirect if it doesn't work, you can check if status == 'success'
    location.href = "index.php";
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅jQuery.post文档.

虽然,我必须问:无论如何你要重定向,为什么要通过ajax?

有什么理由你不想简单地以正常方式提交表格吗?如果没有,你应该走那条路.