• Hi Guys,

    This is my first ajax attempt on my theme using wordpress. I believe i have everything correct but it keeps returning 0 with no data in front. I am aware of the die(‘0’) issue since having this problem, but i don’t think it is even finding my callback.

    My code is below please tell me if you see anything off.

    This is in my page template.

    <?php
    /*
    Template Name: MyGames Controller
    */
    class LP_MyGames {
    	public static function load_scripts(){
    		//load typical ajax functions
    		wp_enqueue_script( 'lp-ajax-functions' );
    		// embed the javascript file that makes the AJAX request
    		wp_enqueue_script( 'lp-ajax-mygames');
    		// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    		wp_localize_script( 'lp-ajax-mygames', 'LPAjaxMyGames', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    		// if logged in:
    		//do_action( 'wp_ajax_' . $_POST['action'] );
    		//do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
    		add_action( 'wp_ajax_test_lp_ajax', 'LP_MyGames::myajax_submit' );
    		//add_action( 'wp_ajax_nopriv_test_lp_ajax', array($this,'myajax_submit') );
    	}
    
    	public static function controller(){
    		include(LP_THEME_INCLUDES_DIR . "/models/class-lp-mygames-model.php");
    
    			get_header();
    			get_sidebar();
    			include("views/mygames.php");
    			get_footer();
    
    	}
    
    	public static function myajax_submit() {
    		global $wpdb;
    		$response = "Category ID is " . $_REQUEST['cat_id'];
    		echo $response;
    		die();
    	}
    }//EOC
    LP_MyGames::load_scripts();
    LP_MyGames::controller();
    ?>

    And this is inside my ajax request file.

    $(function(){
    	$('#main_cat').change(function(){
    	alert("We are inside");
    
    		var data = {
    		action: 'test_lp_ajax',
    		cat_id: $('#main_cat').val()
    		};
    
    		jQuery.post(LPAjaxMyGames.ajaxurl, data, function(response) {
    			alert('Got this from the server: ' + response);
    		});
    
    	});//End of Change function
    
    });

    I’m not sure what the problem is. Any help is appreciated. Thanks.
    PS – the scripts are pre-registered in functions.php prior to this file loading. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax is returning 0 always’ is closed to new replies.