I loaded some contents by ajax, When i try something like this:

$resp = array(
    'success' => true,
    'data' => 'the content here'
);

it’s working without any issues but if i used something like this:

$resp = array(
    'success' => true,
    'data' => get_template_part( 'templates/update', 'profile' )
);

it gives me SyntaxError: JSON.parse: unexpected keyword at line 1 column 1 of the JSON data.

the content here{"success":true,"data":null}

What’s the problem with get_template_part?

3 Answers
3

get_template_part() includes the PHP file, which will break $resp. You need to use output buffering to capture the output into a variable:

ob_start();
get_template_part( 'templates/update', 'profile' );
$data = ob_get_clean();

$resp = array(
    'success' => true,
    'data'    => $data
);

Leave a Reply

Your email address will not be published. Required fields are marked *