【问题标题】:AJAX post didn't workAJAX 帖子不起作用
【发布时间】:2016-12-01 20:56:37
【问题描述】:

我正在尝试在数据库中使用 AJAX 使用 PHP 发布数据,但什么也没发生。我检查了开发者工具是否有任何错误,但没有出现错误,并且 post 请求发送成功

我正常测试了代码以在没有 AJAX 的情况下发布数据并且运行良好,但是使用 AJAX 什么都没有发生

html:

<form id="form_box" action="" method="post">

    <h5>Body:</h5>
    <textarea name="body" cols="30" rows="10"></textarea>

    <div>
        <input type="submit" name="sub_post" value="Post Ajax">
    </div>

</form>

js:

    <script
        src="https://code.jquery.com/jquery-1.12.4.min.js"
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
        crossorigin="anonymous"></script>

<script>
    // Variable to hold request
    var request;

    // Bind to the submit event of our form
    $("#form_box").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: "/functions/post.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!");
           // alert("Post Added successfully");
        });

        // 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();
    });

</script>

php:

<?php
include_once('../includes/config.php');

$post=$db->real_escape_string($_POST['body']);
$db->query("insert into ajax(a_post) values ('$post')");




?>

【问题讨论】:

  • 尝试改用serializeArray
  • $db-&gt;real_escape_string() 你用的是mysql扩展吗?如果是这样,请记住它已损坏且使用不安全。
  • 不,我使用 mysqli

标签: php ajax


【解决方案1】:

我解决了: jquery 代码应该包含 $(document).ready() 处理程序

$(document).ready(function(){
        // Variable to hold request
        var request;

    // Bind to the submit event of our form
    $("#form_box").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: "functions/post.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!");
            // alert("Post Added successfully");
        });

        // 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();
    });
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2018-04-25
    相关资源
    最近更新 更多