【问题标题】:admin-ajax.php not finding an ajax action already definedadmin-ajax.php 找不到已定义的 ajax 操作
【发布时间】:2018-12-11 21:57:13
【问题描述】:

我已经到了不得不在这里寻求你帮助的地步。

在 admin-ajax.php 代码中有一个部分可以检查您注册的 ajax 操作。但是由于某种原因,在触发 Ajax 请求时找不到我的操作。但是,如果我检查我的代码中的操作是否存在。这是检查有效操作的代码部分。

if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) )

这是来自 WordPress 文件 admin-ajax.php 的代码部分:

if ( is_user_logged_in() ) {
    // If no action is registered, return a Bad Request response.
    if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
        wp_die( '0', 400 );
    }

    /**
     * Fires authenticated Ajax actions for logged-in users.
     *
     * The dynamic portion of the hook name, `$_REQUEST['action']`,
     * refers to the name of the Ajax action callback being fired.
     *
     * @since 2.1.0
     */
    do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
    // If no action is registered, return a Bad Request response.
    if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
        wp_die( '0', 400 );
    }

    /**
     * Fires non-authenticated Ajax actions for logged-out users.
     *
     * The dynamic portion of the hook name, `$_REQUEST['action']`,
     * refers to the name of the Ajax action callback being fired.
     *
     * @since 2.8.0
     */
    do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}

您可以在此处查看完整代码:

https://github.com/WordPress/WordPress/blob/master/wp-admin/admin-ajax.php#L155

提前感谢您帮助解决问题。

【问题讨论】:

  • 问题是,你的代码是什么——注册动作的 PHP 和发出 AJAX 请求的 JS?

标签: php ajax wordpress


【解决方案1】:

对于任何路过的人。经过大量调试和测试,我找到了答案。该操作是在继承的类中注册的,因此在代码需要它时它并不存在。因此,在调用了在父构造函数中注册动作的方法后,一切都开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多