简体   繁体   中英

Wordpress ajax admin-ajax.php 400 in Plugin Class

I tried to call an ajax function in my Plugin's Class. But the Console shows a 400 Error on url/wp-admin/admin-ajax.php

I tried adding the ajax hook in the constructor and (as shown here) in the function, but none of them worked. But outside the class, the PHP ajax function works as expected.

(I am completely new to plugin development and OOP .. so please share some best practices if needed)

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).

function run_wps_wc() {

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

    $sync = new Wps_Wc_Sync();

}

run_wps_wc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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