ReferenceError: ajax_object is not defined when loading Wordpress post via Ajax
<p>I've been trying to implement the recommendation by @SagiveSEO in this thread:
<a href="https://wordpress.stackexchange.com/questions/175865/proper-way-to-load-a-single-post-via-ajax" title="Proper way to load a single post via Ajax?">Proper way to load a single post via Ajax?</a></p>

<p>The idea: click a button and load a post via AJAX. The idea being that you'll have a tree of buttons to allow people to navigate quickly to useful content.</p>

<p>Unfortunately, it fails.</p>

<p>In the console I get the message "ReferenceError: ajax_object is not defined" which refers to the line "$.post(ajax_object.ajaxurl..."</p>

<p>What am I doing wrong?</p>

<p>Here's my HTML:</p>

<pre><code>&lt;button class="get_project" data-postid="3300"&gt;PROJECT NAME&lt;/button&gt;
                &lt;div class="postcontainer"&gt;&lt;/div&gt;
</code></pre>

<p>Here's my Javascript:</p>

<pre><code>    jQuery(function($){
    $('.get_project').click(function() {
        var postid = $(this).data('postid'); // Amended by @dingo_d
        $.post(ajax_object.ajaxurl, {
            action: 'my_load_ajax_content ',
            postid: postid
        }, function(data) {
            var $response   =   $(data);
            var postdata    =   $response.filter('#postdata').html();
            $('.postcontainer').html(postdata);
        });

    })
    //alert( "hello world" );
});
</code></pre>

<p>and here is the php from my functions.php file:</p>

<pre><code>function my_load_ajax_content () {

    $pid        = intval($_POST['post_id']);
    $the_query  = new WP_Query(array('p' =&gt; $pid));

    if ($the_query-&gt;have_posts()) {
        while ( $the_query-&gt;have_posts() ) {
            $the_query-&gt;the_post();

            $data = '
            &lt;div class="post-container"&gt;
                &lt;div id="project-content"&gt;
                    &lt;h1 class="entry-title"&gt;'.get_the_title().'&lt;/h1&gt;
                    &lt;div class="entry-content"&gt;'.get_the_content().'&lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            ';

        }
    }
    else {
        echo '&lt;div id="postdata"&gt;'.__('Didnt find anything', THEME_NAME).'&lt;/div&gt;';
    }
    wp_reset_postdata();


    echo '&lt;div id="postdata"&gt;'.$data.'&lt;/div&gt;';
}

// Next two lines corrected - thanks @dingo_d    
add_action ( 'wp_ajax_my_load_ajax_content', 'my_load_ajax_content' );
add_action ( 'wp_ajax_nopriv_my_load_ajax_content', 'my_load_ajax_content' );
</code></pre>

<p>Also required in functions.php within the script enqueuing function:</p>

<pre><code>wp_enqueue_script( 'myajaxpostloader',  get_template_directory_uri().'/js/ajax.js', array( 'jquery' ), '1.0', true );

wp_localize_script( 'myajaxpostloader', 'ajax_object', array(
    'ajaxurl' =&gt; admin_url( 'admin-ajax.php' )
));
</code></pre>

<p>(note that my Javascript is saved as /js/ajax.js where /js/ is a subdirectory of the Wordpress theme).</p>