Note that the constructor of WP_Query
is:
public function __construct( $query = '' ) {
if ( ! empty( $query ) ) {
$this->query( $query );
}
}
so you need a non-empty query input in your current code snippet.
Here's a simple test example:
$query = new WP_Query( ['post_type' => 'post' ] );
wp_send_json_success( $query->posts );
but you can check out e.g. wp_ajax_query_attachments()
to see how WordPress uses WP_Query
in an ajax call.
Also check out the built-in Rest API, it might already have the query you're looking for?