【问题标题】:Wordpress ajax admin-ajax.php 400 in Plugin Class插件类中的 Wordpress ajax admin-ajax.php 400
【发布时间】:2019-04-30 13:07:30
【问题描述】:

我试图在我的插件类中调用一个 ajax 函数。但是控制台在 url/wp-admin/admin-ajax.php 上显示 400 错误

我尝试在构造函数和函数中添加 ajax 钩子(如图所示),但它们都不起作用。但是在类之外,PHP ajax 函数按预期工作。

(我对插件开发和 OOP 完全陌生......所以如果需要,请分享一些最佳实践)

class Wps_Wc_Sync {

 public function get_wc_products() {

   add_action( 'wp_ajax_nopriv_parseCsvAjax', array($this, 'wps_ajax_parseCsvAjax') );
   add_action( 'wp_ajax_parseCsvAjax', array($this, 'wps_ajax_parseCsvAjax') );

        ?>
        <script> 
        jQuery( document ).ready(function($) {
            console.log('ajax');

            parseCsvAjax(0);

            function parseCsvAjax(lastfile = 0) {
                $.ajax({
                type: "POST",
                dataType: 'json',
                url: '/wp-admin/admin-ajax.php',
                data: {
                    action: 'parseCsvAjax',
                    lastfile: lastfile,
                },
                success: function (data) {
                    console.log(data);
                },
                error: function (jqXHT, textStatus, errorThrown) {console.log('Error');}
            });
            }

        });
        </script>
        <?php


    public function wps_ajax_parseCsvAjax($lastfile = 0) {
        echo 'testAJAX1';
        exit();
        return true;
    }

 }
}

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    试试这个。未测试

    <?php
    /*
    Plugin Name: Test
    Version: 1.0
    Plugin URI: 
    Description: test desc
    Author: Vel
    Author URI: Test
    */
    
    class Wps_Wc_Sync {
    
      function  __construct(){
        add_action( 'wp_enqueue_scripts', array($this, 'wpsp_enqueue_scripts_styles')  );
        add_action('wp_ajax_wpsp_admin_ajax_method', array($this, 'wpsb_fnc_ajax_handler'));
        add_action('wp_ajax_nopriv_wpsp_admin_ajax_method', array($this, 'wpsb_fnc_ajax_handler'));
        add_action("wp_footer", array($this, "ajax_call_footer"));
      }
    
        public function wpsp_enqueue_scripts_styles(){
            echo '<script>var wpsp_admin_ajax_url = "'.admin_url("admin-ajax.php").'";</script>';
        }
    
        public function wpsb_fnc_ajax_handler(){
            $gotomethod = trim($_POST['gotomethod']); 
            if(!empty($gotomethod) && method_exists($this, $gotomethod)){
                $rtnval = call_user_method($gotomethod,$this, $_POST); 
                die($rtnval);
            }else
                 die('no-method found');
        }
    
        public function test(){
            print_r($_POST);
            exit;
        }
    
        public function ajax_call_footer(){
        ?>
            <script> 
            jQuery( document ).ready(function($) {
                console.log('ajax');
    
                parseCsvAjax(0);
    
                function parseCsvAjax(lastfile = 0) {
                    jQuery.ajax({
                        type: "POST",                  
                        url: wpsp_admin_ajax_url,                   
                        data: {
                            action: 'wpsb_fnc_ajax_handler',
                            gotomethod:'test',
                            lastfile: lastfile,
                        },
                        success: function (data) {
                            console.log(data);
                        },
                        error: function (jqXHT, textStatus, errorThrown) {console.log('Error');}
                    });
                }
    
            });
            </script>
        <?php
        }
    }
    
    $wps_wc_sync = new Wps_Wc_Sync();
    

    【讨论】:

      【解决方案2】:

      上面的答案不起作用。 但有效的是在我的插件 init(插件的主文件)中调用该类。

      function run_wps_wc() {
      
          $plugin = new Wps_Wc();
          $plugin->run();
      
          $sync = new Wps_Wc_Sync();
      
      }
      
      run_wps_wc();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-08
        • 1970-01-01
        • 2021-02-11
        • 1970-01-01
        相关资源
        最近更新 更多