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

标签 javascript php jquery ajax forms

我的代码中有一个错误,并且我的页面中还包含一个 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/

相关文章:

javascript - jQuery 获取当前元素 (x)html

javascript - 什么是更有效或更短的分组方法,然后使用下划线获取二维数组的百分比值?

javascript - 带有 'Quantity' 下拉列表的 PHP 表单可将输入的数据行插入到表单中

PHP CS Fixer File Watcher 导致 PHPStorm 中的文件缓存冲突

javascript - 带有“下一个”和“上一个”按钮的表单

javascript - 如何在运行时更改对话框的标题?

javascript - CSS:用于触摸屏逆向工程的悬停

javascript - 单选/复选框按钮 "oncheck"使用 javascript 选中/选中时的事件

php - 为什么当我合并 mysql 数据库中的表时,值是重复的?

php - 将 php 变量传递给模态 Twitter Bootstrap 不起作用