在ajax上成功加载php函数

I'm looking for a way to call a php function if an ajax request is successful.

Basic setup code:

wp_register_script( 'theme-follow-me-ajax', ... );   

    wp_localize_script('theme-follow-me-ajax', 'ajax_setting', array(
        'ajax_url'              => admin_url('admin-ajax.php'),
        'ajax_follow_error'     => $this->km_follow_me_error(),
        ....
    ));

    wp_enqueue_script( 'theme-follow-me-ajax' );    

Content to display the error, which should be customizable

 public function km_follow_me_error() {

    $content = esc_html__( 'An error happened. We\'re unable to complete your request.', 'theme' );

    echo apply_filters( 'theme_hook_follow_me_error_message', $content );

}

Ajax call:

$.ajax( {
        url : ajax_setting.ajax_url,
        type : 'post', 
        data: {  
            action                      : 'km_ajax_follow_me',
            security                    : ajax_setting.ajax_nonce,
            ... 
        },  
        success: function( data ) {         
            $('.km-follow-me').html( ajax_setting.ajax_follow_success ).hide().fadeIn( 'slow' );            
            //console.log( ajax_setting.ajax_follow_success );
        },  
    } ) 

WP wp_ajax_ function

public function addon_ajax_follow_me() {

        check_ajax_referer( 'km-ajax-create-nonce', 'security' );   

        ... update user meta ...

        wp_die();   

    }

    $this->loader->add_action( 'wp_ajax_km_ajax_follow_me', $plugin_public, 'addon_ajax_follow_me' );

In console I get the null message, so it's not grabbing the km_follow_me_error function.

Is there a better way?

You're doing it incorrectly. A PHP function cannot be called/accessed from Javascript the way you're trying to do. You have two options to call that function on your AJAX success.

  1. Create/register another AJAX function in WordPress, maybe named as ajax_follow_success and call it in success of the previous AJAX call.

  2. Identify in your first function WordPress AJAX function i.e. addon_ajax_follow_me whether it's a success or failure and call the next function there.