1function my_enqueue() {
2 wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
3 wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
4 }
5 add_action( 'wp_enqueue_scripts', 'my_enqueue' );
6
7//After you can do in js
8jQuery.ajax({
9 type: "post",
10 dataType: "json",
11 url: my_ajax_object.ajax_url,
12 data: formData,
13 success: function(msg){
14 console.log(msg);
15 }
16});