php - Ajax request in wordpress -
i want send ajax request in wordpress tracks clicks. far, have added in functions file:
add_action('init', 'my_script_enqueuer'); function my_script_enqueuer() { wp_register_script("history_script", get_template_directory_uri() . '/js/history_script.js', array('jquery')); wp_localize_script('history_script', 'myajax', array('ajaxurl' => get_template_directory_uri().'/functions.php')); wp_enqueue_script('jquery'); wp_enqueue_script('history_script'); } add_action("wp_ajax_history_trace", "history_trace"); function history_trace() { echo 'fasfasgasgas'; die; }
and in js file :
jquery(document).ready( function() { jquery("#searchsubmit").click( function() { jquery.ajax({ type : "post", datatype : "json", url : myajax.ajaxurl, data : {action: "history_trace"}, success: function(response) { if(response.type == "success") { alert('success') } else { alert("false") } } }) }) })
but in console, request appears in red, , there no response. please help!
in wordpress, use following url process ajax requests:
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
it's important localize script after has been enqueued.
Comments
Post a Comment