gpt4 book ai didi

javascript - AJAX 发布表单不起作用。它绝对没有任何作用

转载 作者:行者123 更新时间:2023-12-01 05:40:12 24 4
gpt4 key购买 nike

我的代码中有一个错误,并且我的页面中还包含一个 js 文件,该文件阻止了 $(document).ready(function () { ...

我正在尝试提交此登录表单:

<form class="form" id="AjaxForm">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit" id="login-button">Login</button>
</form>

通过ajax使用此代码:

var request;
$("#AjaxForm").submit(function(event){

// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);

// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");

// Serialize the data in the form
var serializedData = $form.serialize();

// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);

// Fire off the request to /form.php
request = $.ajax({
url: "login.php",
type: "post",
data: serializedData
});

// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log("Hooray, it worked!");
});

// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});

// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});

// Prevent default posting of form
event.preventDefault();
});

我在这里找到的:jQuery Ajax POST example with PHP

我试图将其发布到login.php,它检查它是否是有效的用户名和密码。但是当我按下登录按钮时,它只是将用户名和密码放入网址中,并且不执行任何操作。当我添加 action="login.php"method="POST" 时,它会提交表单,但不是通过ajax,因为当我注释掉ajax代码时,它仍然会提交。我正在努力阻止这种情况发生。对我的问题有什么见解吗?

编辑:现在住在这里:http://5f6738d9.ngrok.io/test/public/index.html用户名和密码为测试

最佳答案

检查事件是否绑定(bind)在 $(document).on('ready' ... 中,否则事件不会触发,表单将正常提交或不通过 AJAX 提交。

您的代码应如下所示:

$(document).on('ready', function () {
var request;
$("#AjaxForm").submit(function(event){

// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);

// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");

// Serialize the data in the form
var serializedData = $form.serialize();

// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);

// Fire off the request to /form.php
request = $.ajax({
url: "login.php",
type: "post",
data: serializedData
});

// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log("Hooray, it worked!");
});

// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});

// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});

// Prevent default posting of form
event.preventDefault();
});
});

请注意,从 jQuery 1.8 开始,这些回调事件实际上已被弃用。

您还需要确保在所有情况下都在表单上设置了 POSTaction 属性。

关于javascript - AJAX 发布表单不起作用。它绝对没有任何作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232061/

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