Wp-admin/admin-ajax.php 400(错误请求)插件

时间:2020-02-25 作者:Plantas y remedios Caseros

wordpress文件管理ajax有一个奇怪且令人沮丧的行为。php,当我发出ajax请求时,它返回一个错误的404错误请求。

function rp_add_header() {
    if (is_single() || is_page()) { 
        // include the jquery Ajax/form validation javascript file
        wp_enqueue_script( \'ajax-script\', plugin_dir_url(__FILE__).\'rp-script.js\', array(\'jquery\'), 1.0 ); // jQuery will be included automatically

        // create the three Ajax variables used in your template file
        wp_localize_script( \'ajax-script\', \'ajax_object\', array(
                        \'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
                        \'errorEmpty\' => __( \'You need to select one of the options.\' )
                )
        );

        add_action( \'wp_ajax_ajax_action\', \'ajax_action_stuff\' ); // ajax for logged in users
        add_action( \'wp_ajax_nopriv_ajax_action\', \'ajax_action_stuff\' ); // ajax for not logged in users


    }
}
add_action(\'wp_head\',\'rp_add_header\',1);



function ajax_action_stuff() {
    global $mail_report_to;
    $resp[\'status\'] = \'error\';
    $resp[\'errmessage\'] = \'\';
    if (!empty($_POST[\'report-msg\'])) {
        $report_msg = $_POST[\'report-msg\'];
        $report_url = $_POST[\'posturl\'];
        $subject = \'Post report[\'.get_option(\'blogname\').\']\';
        $header = \'From: \'.get_option(\'blogname\').\' <\'.get_option(\'admin_email\').\'>\' . "\\r\\n";
        $message = \'
Someone has reported a post:
Report: \'.$report_msg.\'
Post URL: \'.$report_url.\'
Visitor IP: \'.$_SERVER[\'REMOTE_ADDR\'].\'
Date/time: \'.date(\'Y-m-d H:i:s\');
        if ( wp_mail($mail_report_to, $subject, $message, $header) ) {
            $resp[\'status\'] = \'success\';
            $resp[\'errmessage\'] = \'Your report is submitted, thanks.\';
        } else {
            $resp[\'errmessage\'] = \'Something went wrong, please try again later.\';
        }
    } else {
        $resp[\'errmessage\'] = \'Please select one of the options.\';
    }
    header( "Content-Type: application/json" );
    echo json_encode($resp);
    exit;
}
我已经删除了所有插件,问题依然存在,你有什么想法吗?

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

您只是在内部挂接AJAX响应wp_head():

function rp_add_header() {
    if (is_single() || is_page()) { 
        // etc.

        add_action( \'wp_ajax_ajax_action\', \'ajax_action_stuff\' ); // ajax for logged in users
        add_action( \'wp_ajax_nopriv_ajax_action\', \'ajax_action_stuff\' ); // ajax for not logged in users


    }
}
add_action(\'wp_head\',\'rp_add_header\',1);
但是wp_head 无法运行admin-ajax.php, 所以你的回调永远不会被钩住。您需要将这些挂钩移出该功能:

function rp_add_header() {
    // etc.
}
add_action( \'wp_head\', \'rp_add_header\', 1 );

function ajax_action_stuff() {
    // etc.
}
add_action( \'wp_ajax_ajax_action\', \'ajax_action_stuff\' ); // ajax for logged in users
add_action( \'wp_ajax_nopriv_ajax_action\', \'ajax_action_stuff\' ); // ajax for not logged in users

相关推荐

带有PHP问题的快捷代码“未定义的索引”

我正在创建一个短代码来显示不同大小的图像:大、小和常规。用户可以选择设置large=\"true\" 或small=\"true\" 在编写短代码以输出正确的图像代码时。当短代码处于活动状态时,我在前端遇到了一个PHP问题,下面的消息是large 和/或small 未设置为true或false:注意:未定义索引:大in/wp-content/themes/mysite/functions。php在线1449注意:未定义的索引:在/wp content/themes/mysite/functions中较大。