2 spelling, removed salutation
source | link

Ajax Request not commingcoming back to class

BR,

andre

Ajax Request not comming back to class

BR,

andre

Ajax Request not coming back to class

1
source | link

Ajax Request not comming back to class

I'm struggling with an ajax request. It's executed but not returning back to my class.

My PHP:

    ...
            public function __construct()  {
                add_action( 'admin_print_styles', array($this, 'admin_styles') );
                add_action( 'admin_enqueue_scripts', array($this,'admin_scripts') );
                add_action( 'admin_menu', array($this, 'admin_menu' ));
                $this->dbHandler = DatabaseHandler::get_instance();
                add_action('wp_ajax_nopriv_add_vehicle', array($this,'add_vehicle'));  
                add_action('wp_ajax_add_vehicle', array($this,'add_vehicle'));  
            }

            public function admin_scripts( $hook ){

                wp_enqueue_script( 'admin_scripts', plugins_url( 'js/functions.admin.js', __FILE__ ), array('jquery') );
                wp_localize_script('admin_scripts', 'ajax_var', array(  
                    'url' => admin_url('admin-ajax.php')
                    ,'nonce' => wp_create_nonce('ajax-nonce')  
                ));
            }

            public function add_vehicle()  
            {  
                wp_die("RETURNED"); //NOT GETTING CALLED!
                $nonce = $_POST['nonce'];  

                if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )  
                    die ( 'Busted!');

                $title = $_POST['id'];
                wp_die("TEST" . $title);

                exit;  
            }
    ...

My JS (functions.admin.js:

var addVehicle = function(){

$('.add-vehicle').click(function(){
        var data1 = {
            action: 'add_vehicle',
            nonce: ajax_var.nonce,
            id: 1
        };
        $.ajax({
            type: "post",
            url: ajax_var.url,
            data: data1,
            beforeSend: function(jqXHR, settings){
                console.log(jqXHR);
                console.log(settings);
            },
            success: function(data, textStatus, jqXHR){
                alert('success.');
            },
            error: function(jqXHR, textStatus, errorThrown){
                alert(errorThrown);
            }
        });

        return false;
    });
};

jQuery(document).ready(function($){
    addVehicle();
});

During execution, my AJAX Request was sent successfully. I received the success message. But my add_vehicle function is not called. I've checked a lot of articles, tried many different solutions, but none of them returned back to my class.

BR,

andre