Unable to get_the_content(); of a post in WordPress via AJAX

I’m trying to ajaxify my WordPress theme and I use the method described here and I’m now trying get_the_content of post via functions.php. Using jQuery, when I do alert(data) I get the ‘title’ echo but not the content of the existing post I want (returns 0).

The jQuery part

$('.ajaxed,.ajaxed a,.menu-item-home a,.menu-item-object-page a').live('click', function(event) {
        event.preventDefault();
        var link = $(this).attr('href');
        var toRemove = MySettings.url;
        var rewritepath = link.replace(toRemove,'');
        var handler = function(data) {
            $('title').html($('title', data).html());
            $('#primary').html($('#primary', data).html());
            $('#primary').hide().fadeIn('slow');
            $.address.title(/>([^<]*)<\/title/.exec(data)[1]);
        };
        $.post(ajax_object.ajaxurl, {
            action: 'ajax_action',
            post_id: $(this).find('input.post_id').attr('value')
        },function(data) {
            alert(data.title);
            alert(data.content);
        });
        $.address.state(MySettings.path).crawlable(true).value(rewritepath);
        return false;
    });

The functions.php part

<?php
function javascripts() {
    if( !is_admin()){
        $blogurl = get_bloginfo('url');
        $thumbnail_width = get_option('thumbnail_size_w');
        $thumbnail_height = get_option('thumbnail_size_h');
        $path = parse_url(get_bloginfo('siteurl'), PHP_URL_PATH);
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js';
        wp_deregister_script('jquery');
        if (get_transient('google_jquery') == true) {       
            wp_register_script('jquery', $url, array(), null, true);
        } 
        else {
            $resp = wp_remote_head($url);
            if (!is_wp_error($resp) && 200 == $resp['response']['code']) {
                set_transient('google_jquery', true, 60 * 5);
                wp_register_script('jquery', $url, array(), null, true);
            } 
            else {
                set_transient('google_jquery', false, 60 * 5);
                $url = get_bloginfo('wpurl') . '/wp-includes/js/jquery/jquery.js';
                wp_register_script('jquery', $url, array(), '1.7', true);
            }
        }
        wp_enqueue_script('plugins.js', get_bloginfo('template_directory') . "/js/plugins.js" , array('jquery'));
        wp_enqueue_script('ajax-script', get_bloginfo('template_directory') . "/js/scripts.js", array('jquery'));
        wp_localize_script('ajax-script', 'ajax_object', array('ajaxurl' => admin_url( 'admin-ajax.php' )));
        wp_localize_script('jquery', 'MySettings', array('width' => $thumbnail_width,'height' => $thumbnail_height,'url' => $blogurl,'path' => $path));
    }
}
add_action('wp_enqueue_scripts', 'javascripts');
add_action('wp_ajax_ajax_action', 'ajax_action_stuff'); // ajax for logged in users
add_action('wp_ajax_nopriv_ajax_action', 'ajax_action_stuff'); // ajax for not logged in users
function ajax_action_stuff() {
    $post_id = $_POST['post_id'];

    //Query the post and set-up data
    $post = get_post($post_id);
    setup_postdata();

    update_post_meta($post_id, 'post_key', 'meta_value');

    //Return response as an array. This will give us data.title and data.content browser-side.
    $response = array();
    $response['title'] = get_the_title();
    $response['content'] = get_the_content();

    echo json_encode($response);
    exit;
}
?>

What am I doing wrong? Thanks

Solutions Collecting From Web of "Unable to get_the_content(); of a post in WordPress via AJAX"

get_the_content() must be used inside the loop, or otherwise you’ll need to use setup_postdata(); providing the post object.

Try the following:

function ajax_action_stuff() {
    $post_id = $_POST['post_id'];

    //Query the post and set-up data
    $post = get_post($post_id);
    setup_postdata( $post );

    update_post_meta($post_id, 'post_key', 'meta_value');

    //Return response as an array. This will give us data.title and data.content browser-side.
    $response = array()
    $response['title'] = get_the_title();
    $response['content'] = get_the_content();

    echo json_encode($response);
    exit;
}

This has not been tested

I got a solution here in another post I made about the same problem I had.