What is correct way to send HTML back to an AJAX request in WordPress?
I currently have this:
add_action( 'wp_ajax_nopriv_get-location-info', 'sc_locations_get_location_info' );
add_action( 'wp_ajax_get-location-info', 'sc_locations_get_location_info' );
function sc_locations_get_location_info() {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'get-location-info-nonce' ) ) {
$response = json_encode( array( 'success' => false, 'error' => "Failed nonce check" ) );
} else {
$response = json_encode( array( 'success' => true, 'HTML' => '...LOTS OF WONDERFUL HTML...' ) );
}
header( "Content-Type: application/json" );
echo $response;
exit;
}
Now I'm currently just using json_encode()
but should I be doing anything else to the HTML? The HTML generated can be trusted so no stripping would need to take place.
json_encode
and how to use it in context of ajax and WP? I'm getting confused with things that I found and there isn't a really good example and explanation on how to return an HTML snippet back to JS script.