本文介绍了挂钩一个PHP函数一个AJAX请求:我在想什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该回调消息是0,我想不出为什么。

我试图按照本教程尽可能接近,但我显然失去了一些东西。

在网络上的谷歌浏览器,每次我点击触发表单提交按钮,我看到管理​​-ajax.php 被调用,状态为200.

我是什么做错了吗?

<形式方法=POST行动=会员升级ENCTYPE =的multipart / form-data的&GT ;    < - !......一大堆的投入和东西在这里 - >< /形式GT; 使用error_reporting(-1);ini_set('display_errors设置,论);$ TM =新TeamManager();add_action('wp_ajax_member更新,member_update');功能member_update(){    回声json_en code(TEST ......);} 的jQuery(。成员更新按钮)。点击(函数(){    VAR parentForm = jQuery的(本).closest(形式);    VAR POSTDATA = parentForm.serializeArray();    jQuery.ajax({        网址:?< PHP的回声ADMIN_URL(管理-ajax.php');>中,        数据: {            动作:member_update,            POSTDATA:POSTDATA        },        键入:POST,        数据类型:JSON,        成功:函数(retmsg){            警报(retmsg); //测试现在        },        错误:函数(){            警报(错误); //测试现在        }    });});

解决方案

您的行动挂钩有错误的名称 wp_ajax_member更新和你的动作叫 member_update

那么,你的PHP code应该是这样的:

  add_action('wp_ajax_member_update','member_update');
功能member_update(){
    回声json_en code(阵列('地位'=>'OK'));
    死();
}
 

这是Ajax回调总是需要终止与死亡()在Word preSS声明。

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
        }
    });
});
解决方案

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.

这篇关于挂钩一个PHP函数一个AJAX请求:我在想什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:33