Get PHP values in AJAX to replace input field values

Solution:

You have to return the data as JSON from post_loader function. I’ve cleaned up a little, but still, it’s a mess.

// LOAD DEFAULT VALUES FROM DEFAULT TOUR
add_action('wp_ajax_post_loader', 'post_loader');

function post_loader() {

    $post_id = $_POST["post_id"];

    $args = array(
        'p' => $post_id,
        'numberposts'=> -1,           // Fetch all posts...
        'post_type'=> 'location_tour',      // from the 'location_tour' CPT...
    );

    $location = new WP_Query( $args );

    if ( $location->have_posts() ) : 
        while ( $location->have_posts() ) : 
            $location->the_post();

            $title = the_field('title');
            $description = the_field('description');

            // You have to return data as json
            wp_send_json([
                'title' => $title,
                'description' => $description
            ]);

            //wp_reset_postdata();
        endwhile; 
    endif;  

    // Why do you need this inside this function?
    // add_action('acf/prepare_field/name=default_tour', 'post_loader');  

}

JS

jQuery(document).on( 'click', '#data_fetch', function( dohvati ){
    dohvati.preventDefault();

    var post_id = jQuery('.acf-row .selection .values ul li span').data('id'); // This takes the post ID from the selected Post(Location/Tour) in the Relationship field

    jQuery.ajax({
        url: the_ajax_script.ajaxurl, //The URL that we set for the wordpress admin-ajax.php
        type: "POST",
        dataType: 'json',
        data: {
            action: 'post_loader', // This is the name of the php function
            post_id: post_id,
        },
        success: function(data){
            console.log(data)

            jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeabc041c8a").val(data.title); //This is replacing the title field - but the variables are missing
            jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeab8f41c89").val(data.description); //This is replacing the description field - but the variables are missing

        },
        error: function(error){
            console.log(error)
        },
    });

});