gpt4 book ai didi

javascript - 使用 AJAX 停留在同一页面的未知错误

转载 作者:行者123 更新时间:2023-11-29 21:37:11 24 4
gpt4 key购买 nike

this previous link 之后,我尝试了一个新的 AJAX 代码,但仍然得到相同的结果(数据已正确插入,但不能停留在同一页面中)。

$(function(){
$("#form1").on("submit", function(e){
$.ajax({
url: 'insert.php',
type: 'post',
data: $('#form1').serialize(),
dataType: 'json',
success: function(data) {
alert($('#form1').serialize());
});
e.preventDefault();
});
});

enter image description here

表单有一个 id="form1",表单中的提交按钮是 id="insert"

感谢任何帮助,今天需要解决这个问题。

最佳答案

success 函数应该在关闭 AJAX 函数之前关闭。您忘记添加结束符 }:

$(function(){
$("#form1").on("submit", function(e){
$.ajax({
url: 'insert.php',
type: 'post',
data: $('#form1').serialize(),
dataType: 'json',
success: function(data) {
alert($('#form1').serialize() + "&insert=yes"); // add this
} //<--------------------------------------- You missed this
});
e.preventDefault();
});
});

您正在发送 insert 键作为要发布的参数,但它不起作用,因为您在发送到 AJAX 时没有添加它。所以添加它就可以了。

关于javascript - 使用 AJAX 停留在同一页面的未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566727/

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