0

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.

3
  • Have you tried just echo 'here'; die(); ? Dec 6, 2013 at 16:22
  • Do your localize_script variables look right in the source?
    – vancoder
    Dec 7, 2013 at 1:11
  • @vancoder yes they look right data: "action=add_vehicle&nonce=774b77e381&id=1"& url: "http://localhost/wordpress/wp-admin/admin-ajax.php". ajax_var is recognized an got the values I specified in WP (ajax_var Object {url: "http://localhost/wordpress/wp-admin/admin-ajax.php", nonce: "774b77e381"}). @AndrewBartel yes I did, it don't get called.
    – mybecks
    Dec 17, 2013 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.