jquery - AJAX not showing up in Firebug Console -
disclaimer: not sure wordpress related or not.
i'm following simple tutorial check if ajax works in wordpress localhost.
my ajax-test.js :
jquery(document).ready( function($){ $(".ajax-link").click( function(){ var data = { action: 'test_response', post_var: 'this echoed back' }; $.post(the_ajax_script.ajaxurl, data, function(response) { alert(response); }); return false; }); }); in plugin, add script wp_print_scripts() , add processing function:
function test_ajax_load_scripts(){ wp_enqueue_script("ajax-test", plugin_dir_url( __file__ ) . 'ajax-test.js', array( 'jquery' ) ); wp_localize_script('ajax-test', 'the_ajax_script', array( 'ajaxurl' => admin_url('admin-ajax.php') ) ); } add_action('wp_print_scripts', 'test_ajax_load_scripts'); function text_ajax_process_request(){ if(isset($_post["post_var"])){ $response = $_post["post_var"]; echo $response; die(); } } add_action('wp_ajax_test_response', 'test_ajax_process_request'); i link output href="#" , click , brings me top of page. check firebug , can confirm ajax-test.js loaded in header. there nothing in console.
how else can debug why alert not happening?
your event wire-up function needs e.preventdefault() in it:
$(".ajax-link").click( function(e){ //<--------- e e.preventdefault(); //<-------- preventdefault() var data = { action: 'test_response', post_var: 'this echoed back' }; $.post(the_ajax_script.ajaxurl, data, function(response) { alert(response); }); });
Comments
Post a Comment