duanmaduan1848 2015-07-16 16:18
浏览 44

将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
        }
    });
});
  • 写回答

1条回答

  • dua27031 2015-07-16 21:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能