我在jQuery中有以下Ajax代码,它使用save.php页面将数据从表单保存到MySQL数据库:
$('#frmSurvey').append("<input type='hidden' name='page' value='"+$page+"' />"); // NOTE: this is done before the submit.
document.frmSurvey.submit(); // There is some validation done and then the submit is called
var url = "save.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#frmSurvey").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});以及HTML:
<form id="frmSurvey" name="frmSurvey" action="save.php" method="post" onsubmit="return false">希望这一切都有意义。
我知道Ajax对save.php页面的调用意味着用户看不到页面,并且它发生在后台。然而,save.php页面仍然会出现在web地址栏中一两秒钟--这是正确的吗?有什么方法可以阻止这种情况发生吗?
发布于 2013-08-09 20:29:34
对另一个URL的导航是因为您实际上是在使用document.frmSurvey.submit();进行AJAX调用之前执行适当的表单提交--事实上,这将防止AJAX请求发生,因为导航到表单操作URL将停止当前页面中的JS处理。
如果删除该行,则表单将通过AJAX提交,无需任何重定向。
https://stackoverflow.com/questions/18154672
复制相似问题