-1

I have added a custom link in WooCommerce Menu using this thread Add custom my account menu item based on user role in WooCommerce 3+ and Its working fine as per my expectations.

Now I have select2 library and in my new custom page http://localhost/wordpress/my-account/clients/ I have added a dropdown with having the ability to select multiple.

And below is my code through which I am detecting if the user deletes an item from the dropdown and then I am firing the below event.

jQuery('.clientchildren').on('select2:unselect', function (evt) {  
  
  var userId = evt.params.data.id;     
  
  jQuery.ajax({
              type: "POST",
              url: '/wp-admin/admin-ajax.php',  
              dataType: 'json',
              delay: 250, // delay in ms while typing when to perform a AJAX search
              data : {
                        action : 'remove_client_children_user_id',
                        userId : userId,                        
              },                       
              success: function(data)
              {  
                var data = JSON.parse(data);      
                toastr["success"]("Removed Successfully." );          
                
              }
            });

});

And In my functions.php file I have put the below code.

functions.php

add_action( 'wp_ajax_nopriv_remove_client_children_user_id', 'remove_client_children_user_id' );
add_action( 'wp_ajax_remove_client_children_user_id', 'remove_client_children_user_id' );

function remove_client_children_user_id() {
    

    echo json_encode(array('status'=>1),true);
    exit;
}

But it never goes inside and it keeps saying

The requested URL was not found on this server.

enter image description here

enter image description here

I even tried to use ajax calls by changing action with some of my old existing functions like wp_ajax_get_mindesk_var_clients by changing the action to get_mindesk_var_clients but still, it never goes inside to that as well.

Is it happening because I have created a new custom page and trying to achieve an ajax call to it?

Can someone guide me what should I do to make it work and where exactly it's having a problem calling ajax?

Thanks

Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70

1 Answers1

2

You can use wp_localize_script to pass ajax URL to js. check the below code.

function theme_enqueue_scripts() {
    wp_register_script( 'custom', get_stylesheet_directory_uri() . '/assets/js.custom.js', array('jquery'), null, true );
    
    wp_localize_script( 'custom', 'custom_object',
        array( 
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
        )
    );

    wp_enqueue_script( 'custom' );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );

jQuery('.clientchildren').on('select2:unselect', function (evt) {  
    var userId = evt.params.data.id;     
    jQuery.ajax({
        type: "POST",
        url: custom_object.ajaxurl,
        dataType: 'json',
        delay: 250, // delay in ms while typing when to perform a AJAX search
        data : {
            action : 'remove_client_children_user_id',
            userId : userId,                        
        },                       
        success: function(data){  
            var data = JSON.parse(data);      
            toastr["success"]("Removed Successfully." );          
        }
    });
});
Bhautik
  • 11,125
  • 3
  • 16
  • 38