将PHP函数连接到AJAX请求:我缺少什么?

The callback message is 0 and I can't figure out why.

I am trying to follow this tutorial as closely as possible, but I'm obviously missing something.

In Network on Google Chrome, each time I click the button that triggers the form to submit, I see that admin-ajax.php is being invoked and the status is 200.

What am I doing wrong?

<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
        }
    });
});
duanneng2014
duanneng2014 只是在黑暗中拍摄,但不应该是数据:{action:'wp_ajax_member-update',postData:postData},?
5 年多之前 回复
douzhu6149
douzhu6149 你在哪里回应什么?在member_update()???如果是,那么你在哪里调用member_update()函数?
5 年多之前 回复
duanaozhong0696
duanaozhong0696 你告诉JS期待json,但你在PHP中有最大的错误报告/显示错误。这意味着任何php警告/错误都将成为输出的一部分,并被视为json语法错误。
5 年多之前 回复

1个回答

Your action hook has the wrong name wp_ajax_member-update and your action is called member_update

So, your PHP code should look like this:

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

An Ajax callback always needs to end with a die() statement in WordPress.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐