gpt4 book ai didi

php - 使用 jQuery 通过 AJAX 提交表单并显示处理 PHP 页面的返回

转载 作者:行者123 更新时间:2023-12-01 02:49:37 26 4
gpt4 key购买 nike

我基本上只是尝试通过 AJAX 提交表单,并希望显示处理 PHP 页面的返回结果。 PHP 页面仅验证输入并显示发生的错误或向用户显示感谢消息。通常我不会使用 ajax,但在这种情况下,客户设置要求我这样做。

尽管听起来很简单,但我无法让它工作。

我正在使用的 jquery (1.4.2) 代码:

$(document).ready(function() {
$('#subscription').submit( function() {
var inputdata = $('#subscription').serialize();
alert(inputdata);
$.post("process_form.php", inputdata, function(data) {alert(data);} );
});
});

Firebug 只是显示:

POST process_form - status: aborted

另一个奇怪的观察是,成功处理程序实际上被调用,但没有显示responseText。我的 apache 日志显示了流程表单的 304 http 消息,之后显示了另一行表单的状态代码 200,就好像它被重定向回那里一样,我不知道这是否是正常的 AJAX 行为。

W3C 声明 here :

If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request event rules. Otherwise, this is a network error.

但我不认为我的请求违反了其中任何一个。

无论如何,长话短说:我被困住了。

我们将非常感谢任何帮助,当然我很乐意提供任何进一步需要的信息。

最佳答案

尝试在提交处理程序末尾返回false。我不确定发生的事情的精确顺序,但如果您不返回 false,则不会阻止表单的默认操作。因此,表单将按正常方式提交,XHR 将崩溃并导致大爆炸。

关于php - 使用 jQuery 通过 AJAX 提交表单并显示处理 PHP 页面的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3651553/

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