php - 使用 Ajax 的新网址
问题描述
我正在学习 Ajax,但我不明白如何做某事:
我有一个表单,在提交表单时,页面不应该重新加载。我看过很多关于如何做到这一点的例子,但它们都在 HTML 中给出了视觉响应。是否可以提供一个新的 PHP url 作为响应。例如,如果我之前有 index.php,在提交 from(不重新加载页面)之后,我会有 index.php?name=Alex
使用www.DeepL.com/Translator翻译(免费版)
解决方案
当您不希望您的提交刷新页面时,您应该使用:
e.preventDefault();
并将点击返回为假。例子:
$('#submit-button').click(function(e){
e.preventDefault();
$.ajax({
url: 'index.php',
type: 'GET',
data: {
name: 'alex' // input value
},
error: function(XMLHttpRequest, textStatus, errorThrown) {},
success: function(data) {
}
});
return false; // stop the actual form post !important!
});