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

在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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vue3为什么请求在这边获取不到数据呢(关键词-select)
      • ¥20 Multisim仿真设计三路智力抢答装置
      • ¥15 用UML制作简单的语音计算机
      • ¥20 用socket实现ping遇到的问题
      • ¥15 综合的类,这个类里面里面又有构造方法,重载方法,有继承,有重写,有接口
      • ¥15 电脑重置失败无法开机也进不了修复模式,如何解决?
      • ¥100 px4飞控如何设置平飞模式下飞机固定的仰角
      • ¥15 关于#python#的问题:不可以使用max ,min ,exit ,break 的情况下找出哪一年份出生率最高,以及哪一年份出生率最低
      • ¥15 WPF中使用DoDragDrop程序会崩,"FatalExecutionEngineError"
      • ¥15 C语言 堆排序 数据结构