简体   繁体   English

插件类中的Wordpress ajax admin-ajax.php 400

[英]Wordpress ajax admin-ajax.php 400 in Plugin Class

I tried to call an ajax function in my Plugin's Class. 我试图在我的插件类中调用ajax函数。 But the Console shows a 400 Error on url/wp-admin/admin-ajax.php 但是控制台在url/wp-admin/admin-ajax.php上显示400错误

I tried adding the ajax hook in the constructor and (as shown here) in the function, but none of them worked. 我尝试在构造函数中添加ajax钩子并在函数中添加(如此处所示),但它们都没有工作。 But outside the class, the PHP ajax function works as expected. 但是在课外,PHP ajax函数按预期工作。

(I am completely new to plugin development and OOP .. so please share some best practices if needed) (我是插件开发和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;
    }

 }
}

Try this. 尝试这个。 Not tested 没有测试过

<?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();

The answer above did not work. 上面的答案没有用。 But what worked, was to call the class in my plugin init (plugin's main file). 但有用的是在我的插件init(插件的主文件)中调用该类。

function run_wps_wc() {

    $plugin = new Wps_Wc();
    $plugin->run();

    $sync = new Wps_Wc_Sync();

}

run_wps_wc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM