Managed to fix this, as @vancoder mentioned that the wp_die was causing there to still be a response and said to use die() instead. This didn’t work, so instead I changed the way the json was sent.
By using wp_send_json() uinstead, we don’t need to use die() or wp_die() at the end of the function as wp_send_json() includes wp_die() within it.
which all seems to work well.
update the callback in functions.php
if ( $search_query->have_posts() ) {
$result = array();
while ( $search_query->have_posts() ) {
$search_query->the_post();
$cats = strip_tags( get_the_category_list(", ") );
$result[] = array(
"id" => get_the_ID(),
"title" => get_the_title(),
"content" => get_the_content(),
"permalink" => get_permalink(),
"year" => get_field('year'),
"rating" => get_field('rating'),
"director" => get_field('director'),
"language" => get_field('language'),
"coursecats" => $cats,
"imageUrl" => wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()),'full')
);
}
wp_reset_query();
//echo json_encode($result); - Old way
wp_send_json($result); // new way (includes wp_die())
} else {
}
}