簡體   English   中英

將PHP函數連接到AJAX請求:我缺少什么?

[英]Hooking up a PHP function to an AJAX request: What am I missing?

回調消息為0,我不知道為什么。

我正在嘗試盡可能緊密地遵循本教程 ,但顯然缺少某些內容。

在Google Chrome上的網絡中,每次我單擊觸發表單提交的按鈕時,都會看到正在調用admin-ajax.php ,狀態為200。

我究竟做錯了什么?

<form method="POST" action="member-update" enctype="multipart/form-data">
    <!-- ... bunch of inputs and stuff in here -->
</form>
error_reporting(-1);
ini_set('display_errors', 'On');
$tm = new TeamManager();
add_action('wp_ajax_member-update', 'member_update');
function member_update() {
    echo json_encode("TEST ... ");
}
jQuery('.member-update-button').click(function () {
    var parentForm = jQuery(this).closest('form');
    var postData = parentForm.serializeArray();
    jQuery.ajax({
        url: "<?php echo admin_url('admin-ajax.php'); ?>",
        data: {
            action: 'member_update',
            postData: postData
        },
        type: "POST",
        dataType: 'json',
        success: function (retmsg) {
            alert(retmsg); // test for now
        },
        error: function () {
            alert("error"); // test for now
        }
    });
});

您的動作掛鈎名稱錯誤wp_ajax_member-update ,您的動作稱為member_update

因此,您的PHP代碼應如下所示:

add_action('wp_ajax_member_update', 'member_update');
function member_update() {
    echo json_encode(array('status' => 'ok'));
    die();
}

Ajax回調始終需要在WordPress中以die()語句結尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM