• jQuery(document).ready(function ($) {
    	$('#upd-role').change(function () {
    		console.log('aaa');
    		var this2 = this;
    		$.post(
    			my_ajax_obj.ajax_url,
    			{
    				_ajax_nonce: my_ajax_obj.nonce,
    				action: "ajax_handler_hook",
    				role: this2.value
    			}, function (data) {
    				console(data);
    			}
    		);
    	});
    });
    
    
    require_once ABSPATH . '/wp-includes/formatting.php';
    
    register_activation_hook( file: __FILE__, callback: 'ext_role_menu_activate_plugin' );
    
    register_deactivation_hook( file: __FILE__, callback: 'ext_role_menu_deactivate_plugin' );
    
    register_uninstall_hook( file: __FILE__, callback: 'ext_role_menu_uninstall_plugin' );
    
    function ext_role_menu_activate_plugin() : void
    {
    	$option = getopt( short_options: 'extend_role_menu_plugin' );
    	if ( ! $option ) {
    		add_option( option: 'extend_role_menu_plugin', value: 1 );
    	}
    }
    
    function ext_role_menu_deactivate_plugin()
    {
    	$option = getopt( short_options: 'extend_role_menu_plugin' );
    	if ( $option ) {
    		update_option( option: 'extend_role_menu_plugin', value: 0 );
    	}
    }
    
    function ext_role_menu_uninstall_plugin() : void
    {
    	$option = getopt( short_options: 'extend_role_menu_plugin' );
    	if ( $option ) {
    		delete_option( option: 'extend_role_menu_plugin' );
    	}
    }
    
    // Hook for adding admin menus
    add_action( 'admin_menu', 'rl_add_pages' );
    
    // action function for above hook
    function rl_add_pages() : void
    {
    	add_menu_page( page_title: __( 'Show Roles', 'extend-role-menu' ), menu_title: __( 'Show Roles', 'extend-role-menu' ), capability: 'manage_options', menu_slug: 'show-roles', callback: 'show_roles_func' );
    	add_submenu_page( parent_slug: 'show-roles', page_title: __( 'Add Role', 'extend-role-menu' ), menu_title: __( 'Add Role', 'extend-role-menu' ), capability: 'manage_options', menu_slug: 'add-role', callback: 'add_role_func' );
    	add_submenu_page( parent_slug: 'show-roles', page_title: __( 'Update Role', 'extend-role-menu' ), menu_title: __( 'Update Role', 'extend-role-menu' ), capability: 'manage_options', menu_slug: 'upd-role', callback: 'upd_role_func' );
    	add_submenu_page( parent_slug: 'show-roles', page_title: __( 'Delete Role', 'extend-role-menu' ), menu_title: __( 'Delete Role', 'extend-role-menu' ), capability: 'manage_options', menu_slug: 'del-role', callback: 'del_role_func' );
    }
    
    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    
    function my_enqueue( $hook ) : void
    {
    	// var_dump(value: $hook);
    	if ( 'extend-role-menu.php' !== $hook ) {
    		return;
    	}
    
    	wp_enqueue_script( handle: 'myScripts', src: plugins_url( path: '/js/myScripts.js', plugin: __FILE__ ), deps: array( 'jquery' ), ver: '1.0.0', in_footer: true );
    
    	wp_localize_script( handle: 'myScripts', object_name: 'my_ajax_obj',
    	l10n: array(
    			'ajax_url' => admin_url( 'admin-ajax.php' ),
    			'nonce'    => wp_create_nonce( 'my-extend-role-menu-nonce' ),
    		)
    	);
    }
    
    add_action( 'wp_ajax_ajax_handler_hook', 'ajax_handler' );
    
    function ajax_handler()
    {
    	$nonce    = check_ajax_referer( 'ajax_handler_hook', 'my-extend-role-menu-nonce', true );
    	$the_role = get_role( wp_unslash( $_POST[ 'role' ] ) );
    	if ( $the_role ) {
    		$capabilities = $the_role->capabilities;
    		if ( ! empty( $capabilities ) ) {
    			wp_send_json_success( $capabilities );
    		}
    		return wp_send_json_error( false );
    	}
    }

    Why i cannot get the data back on jquery via the server’s response? I cannot even get an alert() when the document loads. Any suggestion??

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You forgot .log for the success callback:

    function (data) {
        console.log(data);
    }

    I’m not sure what sort of constructs like handle: are for function parms. I’ve never seen it. Not that I’ve seen everything, but try removing them:

    wp_enqueue_script('myScripts', plugins_url('/js/myScripts.js', __FILE__ ),  array( 'jquery' ), '1.0.0', true );
    
    	wp_localize_script( 'myScripts', 'my_ajax_obj',
    	   array( //etc....

    With those changes in a quick test I at least get a response. My test doesn’t include adequate context to say for sure, but it looks like your code should work with these changes.

Viewing 1 replies (of 1 total)
  • The topic ‘jquery ajax response not getting data’ is closed to new replies.