duanla8800 2019-01-28 15:23
浏览 150
已采纳

在WordPress多站点用户前端页面中POST“admin-ajax.php”400

I'm trying to use AJAX in the user frontend pages from a plugin in WordPress Multisite but the admin-ajax.php generates Bad Request 400 and always fails.

  1. Developing WordPress Plugin - AJAX in admin pages working good
  2. WordPress Multisite
  3. Test on Site_1

This is a part of my code and the output, I just shorten it, There's a class to instantiate the AJAX actions and another one after it to include the JS file using wp_enqueue_scripts action ..

Anyway the JS file included correctly and console.log function displaying all JS data object as you see in the code below, BUT it always fails and msg argument print Bad Request in statusText .. What I missed??

/* == AJAX Class == */
add_action( 'wp_ajax_wpmu_frontendpage', array( &$this, '_class_function' ) );
add_action( 'wp_ajax_nopriv_wpmu_frontendpage', array( &$this, '_class_function' ) );


/* 1. The JS file included correctly */
wp_register_script( 'wpmu/scripts/frontend/core', 'JS_FILE.js', 
    array( 'jquery' ), '1.0', true );

/* 2. Localize */
wp_localize_script( 'wpmu/scripts/frontend/core', 'wpmu_ajax', array (
    'ajax_url'  => esc_url( admin_url('admin-ajax.php') )
  )
);

/* 3. Enqueue */
wp_enqueue_script( 'wpmu/scripts/frontend/core' );


== JS FILE ==
(function ($) {
'use strict';
$.wpmu = $.wpmu || {};
$.wpmu.ajax_vars = {
    ajax_url: wpmu_ajax.ajax_url,
};
$(document).ready(function () {
    /* 1. DEBUG */
    console.log($.wpmu.ajax_vars);
    /* 2. DEBUG - OUTPUT */
    -- ajax_vars:
    --    ajax_url: "http://localwp.com:90/wpmu/SITE_1/wp-admin/admin-ajax.php"

    /* ========= Main Code ======== */
    $(document.body).on('click', '#my-wrapper .submit', function (e) {
        e.preventDefault();

        var server_data = {
            action: 'wpmu_frontendpage',
        };

        $.ajax({
            method: 'POST',
            async: true,
            url: $.wpmu.ajax_vars.ajax_url,
            data: server_data,

        }).done(function (response) {
            alert('success');
        }).fail(function (msg) {
            console.log( msg );
        });

        return false;

    });
});

})(jQuery);
  • 写回答

1条回答 默认 最新

  • dougai2427 2019-01-28 19:01
    关注

    To help others, I found the solution, That's because I made an IF condition check if not admin page and the {wp_ajax_} actions MUST triggered globally, I thought that it's good to instantiate the AJAX class when it needed in the frontend user pages only.

    -- Old Code -- WRONG

    if( ! is_admin() ) {
       /* Instantiate the AJAX class */
       add_action( 'wp_ajax_wpmu_frontendpage', array( &$this, '_class_function' ) );
       add_action( 'wp_ajax_nopriv_wpmu_frontendpage', array( &$this, '_class_function' ) );
    }
    

    -- Fix

    /* Instantiate the AJAX class without NON-Admin pages condition */
    add_action( 'wp_ajax_wpmu_frontendpage', array( &$this, '_class_function' ) );
    add_action( 'wp_ajax_nopriv_wpmu_frontendpage', array( &$this, '_class_function' )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵